一、先修改服務器的配置文件 1、Master服務器配置簡單,修改my.cnf爲: server-id = 1 log-bin=n01-bin relay-log=ns01-relay-bin set-variable=binlog-ignore-db=mysql 2、slave1的配置加入 master-host = 172.16.20.135 master-user = rep master-password = cnrep master-port = 3306 server-id = 2 log-bin=n01-bin replicate-do-db =mydnsreplicate-ignore-db=mysql log-slave-updates slave-net-timeout=60 master-connect-retry=10 log-slave-updates 3、slave2服務器的配置 master-host = 172.16.20.3 master-user = rep1 master-password = cnrep master-port = 3306 server-id = 3 log-bin=n01-bin replicate-do-db =mydnsreplicate-ignore-db=mysql log-slave-updates slave-net-timeout=60
master-connect-retry=10 log-slave-updates 二、重啓master數據庫
三、然後鎖定master數據庫的表: FLUSH TABLES WITH READ LOCK; 四、在master數據庫中添加用於slave1同步的用戶,並賦予相關權限: GRANT REPLICATION SLAVE ON *.* TO rep@sa_cfengine1 IDENTIFIED BY ‘cnrep’; GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO rep@sa_cfengine1 IDENTIFIED BY ‘cnrep’;
五、在slave1數據庫中添加用於slave2同步的用戶,並賦予相關權限: GRANT REPLICATION SLAVE ON *.* TO rep1@sa_cfengine2 IDENTIFIED BY ‘cnrep’; GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO rep1@sa_cfengine2 IDENTIFIED BY ‘cnrep’;
六、同步數據庫: 方法很多,可以打包之後scp,再解壓,由於sa_cfengine1到mysql master服務器通道打通了,切sa_cfengine2到sa_cfengine1通道也打了,故直接scp整個數據庫目錄即可。 注意:此時要注意刪除同步過來的日誌文件,最好把與數據庫無關的文件全刪除(可以將非目錄的文件全刪了)。
七、重啓salve1的mysql,起來之後鎖定表
八、重啓slave2的mysql,然後先後給slave1和master服務器的mysql表解鎖 UNLOCK TABLES;
九、分別登錄slave1和slave2的mysql,查看同步狀態: SHOW SLAVE STATUS\G
*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.20.135 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000051 Read_Master_Log_Pos: 13856842 Relay_Log_File: sa_cfengine1-relay-bin.000013 Relay_Log_Pos: 624419 Relay_Master_Log_File: mysql-bin.000051 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: AliSMS,lcd,loginmanager,samis,sareport,syslog,web_speed Replicate_Ignore_DB: mysql,mysql 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: 13856842 Relay_Log_Space: 624419 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 1 row in set (0.01 sec) 注意標註爲紅色的地方,兩個都是yes說明一切正常,否則要檢查原因,可以看error log查找原因後做相應的處理。
十、測試: 在master數據庫中update在同步列表中的一個表的一個字段,如果slave服務器的做相應改變,則測試用過。
實例: 67: mysql> show master status \G; 66 mysql> show slave status \G; .5 (既做67的從,有做10的主) mysql> show master status; 10. CHANGE MASTER TO MASTER_LOG_FILE='n01-bin.000004', MASTER_LOG_POS=220; |