[root@localhost ~]# mysql -uroot -pwsyht123 -e "show variables like ‘log_bin’;" #查看有沒有開啟log-bin +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | ON | +---------------+-------+ [root@master ~]# service mysqld restart
3、主服務器端建立同步測試數據庫 創建庫和表、在master服務器上授權一個用戶可以從slave 服務器上連接自已,且有拷貝數據的權限
[root@master ~]# mysql -uroot -pwsyht123 mysql> grant replication slave on *.* to 'wsyht'@'192.168.2.%' identified by '123456'; mysql> flush privileges; #刷新權限 mysql> flush table with read lock; #讀鎖,不讓別的用戶往裡寫,當前窗口還不能斷 mysql> show master status; #查看master狀態 +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000028 | 107 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) mysql> show databases; #查看Master當前數據庫有什麼庫 +--------------------+ | Database | +--------------------+ | information_schema | | d1 | | mysql | | performance_schema | +--------------------+ 5 rows in set (0.00 sec)
4、備份當前所有庫所有表(克隆多一個窗口進行如下操作) [root@master ~]# mysqldump -uroot -pwsyht123 -A -B --events > /opt/all.$(date +%F).sql #開多一個窗口進行全庫備份 5、拷貝備份文件到slave從服務器 [root@master ~]# rsync -avzP -e "ssh -p 22" /opt/all.$(date +%F).sql [email protected]:/opt/ 【Slave配置】 1、修改server-id號 [root@slave ~]# cat /etc/my.cnf [mysqld] ... server-id=2 ... 2、重啟服務 [root@slave ~]# mysql -uroot -pwsyht123 -e "show variables like 'log_bin';" #查看有沒有開啟log-bin [root@slave ~]# service mysqld restart 3、導進數據庫
[root@slave ~]# mysql -uroot -pwsyht123 -e "show databases;" #查看當前slave有什麼庫 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.00 sec) [root@slave ~]# mysql -uroot -pwsyht123 < /opt/all.$(date +%F).sql #將主庫的備份數據導進從庫 [root@slave ~]# mysql -uroot -pwsyht123 -e "show databases;" #導進庫後再次查看 +--------------------+ | Database | +--------------------+ | information_schema | | d1 | | mysql | | performance_schema | +--------------------+
4、測試使用授權用戶是否能連接登陸主數據庫服務器 [root@slave ~]# mysql -h192.168.2.1 -uwsyht -p123456 mysql> quit 5、建立同步賬戶的信息
[root@slave ~]# mysql -uroot -pwsyht123 mysql> change master to master_host='192.168.2.1', #連接的主機 master_user='wsyht', #連接同步Master的賬戶 master_password='123456', #連接同步Master的密碼 master_log_file='mysql-bin.000028', #Master的現記錄的日志文件 master_log_pos=107; #Master記錄的日志點 mysql> start slave; mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.2.1 Master_User: wsyht Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000028 #Master正在使用的日志文件 Read_Master_Log_Pos: 107 #Master正中使用的日志點 Relay_Log_File: mysql-relay-bin.000002 #Slave正使用的中繼日志文件 Relay_Log_Pos: 253 #Slave中繼日志文件的日志點 Relay_Master_Log_File: mysql-bin.000028 Slave_IO_Running: Yes #IO線程呈YES為正常 Slave_SQL_Running: Yes #SQL線程呈YES為正 Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 107 Relay_Log_Space: 409 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.00 sec)
ERROR:
No query specified mysql> show databases; #查看Slave當前數據庫有什麼庫 +--------------------+ | Database | +--------------------+ | information_schema | | d1 | | mysql | | performance_schema | +--------------------+ 5 rows in set (0.00 sec)
【測試同步】 1)Master配置 1、解鎖 mysql> unlock tables; #解鎖表,主庫從新提供服務 2、創建新庫
mysql> create database test; #創建test數據庫測試同上海 Query OK, 1 row affected (0.00 sec) mysql> show databases; #查看Master當前數據庫有什麼庫 +--------------------+ | Database | +--------------------+ | information_schema | | d1 | | mysql | | performance_schema | | test | +--------------------+ 6 rows in set (0.00 sec)
2)Slave配置 1、查看有沒有同步數據庫
mysql> show databases; #查看Slave當前數據庫有什麼庫 +--------------------+ | Database | +--------------------+ | information_schema | | d1 | | mysql | | performance_schema | | test | +--------------------+ 6 rows in set (0.00 sec)
#看到上面slave表的信息可看到有test,主從同步成功,實驗完成