master—>slave1—>slave2mysql
master須要開啓二進制日誌linux
中間的slave1也須要打開二進制日誌,可是它默認不把應用master的操做記錄到本身的二進制日誌。因此須要打開一個參數讓它記錄,才能夠傳給第三級的從;而後在中間從和第三級從之間再作一次AB複製就能夠了。sql
打開log-slave-updates=1,讓第一臺傳過來relay日誌記錄到本身的二進制日誌。數據庫
思路:vim
先搭建好主從——>而後加入slave2服務器
環境準備及要求:架構
一、關閉防火牆和selinuxide
二、hosts文件中三臺服務器主機名和ip地址一一對應起來測試
三、系統時間須要同步spa
四、master和slave的數據庫版本保持一致(系統版本保持一致)
思路:
先搭建好主從——>而後加入slave2
一、master和中間slave1必須開啓二進制日誌
二、slave2必須開啓中繼日誌
三、master和slave1和slave2的server-id必須不同 範圍(2^23-1)
四、master和slave的初始數據一致
環境:
master:192.168.1.166
slave1:192.168.1.114
slave2:192.168.1.180
一、環境準備
# master: [root@master ~]# vim /etc/hosts 192.168.1.114 slave1 # slave1: [root@slave1 ~]# vim /etc/hosts 192.168.1.166 master 192.168.1.180 slave2 # slave2: [root@slave2 ~]# vim /etc/hosts 192.168.1.114 slave1
二、修改配置文件
# master: root@master ~]# vim /etc/my.cnf log-bin=mysql-bin server-id = 1 # slave1: [root@slave1 ~]# vim /etc/my.cnf log-bin=mysql-bin log-slave-updates=1 server-id = 2 relay-log=/data/DB/relay.log # slave2: [root@slave2 ~]# vim /etc/my.cnf server-id = 3 relay-log=/data/DB/relay.log
三、數據初始化一致
狀況1:master和slave都沒有業務數據,直接初始化master和slave的數據庫
狀況2:master上面有業務數據
masteer: 1>中止數據庫 [root@master ~]# /etc/init.d/mysqld stop 2>物理級別備份 [root@master ~]# cd /data/DB/ && ll [root@master DB]# tar -cvzf /tmp/mysql.tar.gz * [root@master DB]# scp /tmp/mysql.tar.gz 192.168.1.114:/tmp [root@master DB]# scp /tmp/mysql.tar.gz 192.168.1.180:/tmp 3>slave1和slave2進行數據恢復 [root@slave1 ~]# cd /data/DB/ && ll [root@slave1 DB]# tar -xf /tmp/mysql.tar.gz -C . [root@slave1 DB]# /etc/init.d/mysqld start [root@slave2 ~]# cd /data/DB/ && ll [root@slave2 DB]# tar -xf /tmp/mysql.tar.gz -C . [root@slave2 DB]# /etc/init.d/mysqld start 4>查看slave和master上面的數據是否同樣 [root@master DB]# echo "show databases;" | mysql -p123 [root@slave1 DB]# echo "show databases;" | mysql -p123 [root@slave2 DB]# echo "show databases;" | mysql -p123
四、在master上面建立對slave1受權用戶及查看binlog信息
1>建立受權用戶 mysql> grant replication slave on *.* to 'slave'@'192.168.1.114' identified by '123'; 2>查看binglog信息,只有打開二進制日誌,這句命令纔有結果,表示當前數據庫的二進制日誌寫到什麼位置 mysql> show master status \G *************************** 1. row *************************** File: mysql-bin.000013 Position: 331 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.03 sec)
五、在slave1上配置同步信息
slave1: mysql> change master to master_host='192.168.1.166', master_user='slave', master_password='123', master_port=3306, master_log_file='mysql-bin.000013', master_log_pos=331; 參數說明: master_host: master ip master_user: 同步用戶 master_password: 密碼 master_port: 端口 master_log_file: master上面查看到的二進制日誌名 master_log_pos: master上面查看到的POS值 mysql> start slave; mysql> show slave status \G; Slave_IO_Running: Yes Slave_SQL_Running: Yes 若是查看狀態這兩個值都爲Yes, 說明M--S就已經作好
六、在slave1上面對slave2受權
先中止slave slave1: mysql> stop slave; Query OK, 0 rows affected (0.03 sec) mysql> grant replication slave on *.* to 'slave'@'192.168.1.180' identified by '123'; mysql> show master status \G *************************** 1. row *************************** File: mysql-bin.000017 Position: 425 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec)
七、在slave2上配置同步信息
slave2: mysql> change master to master_host='192.168.1.114', master_user='slave', master_password='123', master_port=3306, master_log_file='mysql-bin.000017', master_log_pos=425; mysql> start slave; mysql> show slave status \G; Slave_IO_Running: Yes Slave_SQL_Running: Yes
八、在回到slave1上面啓動slave
slave1: mysql> start slave;
九、測試驗證
先查看原有的三個服務器的db01庫中的table03表, master: mysql> select * from db01.table03; +------+------+ | id | name | +------+------+ | 1 | haha | | 2 | wowo | | 4 | yoyo | | 1 | haha | | 2 | wowo | | 3 | lili | | 4 | yoyo | +------+------+ 7 rows in set (0.10 sec) slave1: mysql> select * from db01.table03; +------+------+ | id | name | +------+------+ | 1 | haha | | 2 | wowo | | 4 | yoyo | | 1 | haha | | 2 | wowo | | 3 | lili | | 4 | yoyo | +------+------+ 7 rows in set (0.17 sec) slave2: mysql> select * from db01.table03; +------+------+ | id | name | +------+------+ | 1 | haha | | 2 | wowo | | 4 | yoyo | | 1 | haha | | 2 | wowo | | 3 | lili | | 4 | yoyo | +------+------+ 7 rows in set (0.44 sec) 在master上面刪除一條數據 mysql> delete from db01.table03 where name="lili"; Query OK, 1 row affected (0.38 sec) 再次查看 master: mysql> select * from db01.table03; +------+------+ | id | name | +------+------+ | 1 | haha | | 2 | wowo | | 4 | yoyo | | 1 | haha | | 2 | wowo | | 4 | yoyo | +------+------+ 6 rows in set (0.00 sec) slave1: mysql> select * from db01.table03; +------+------+ | id | name | +------+------+ | 1 | haha | | 2 | wowo | | 4 | yoyo | | 1 | haha | | 2 | wowo | | 4 | yoyo | +------+------+ 6 rows in set (0.00 sec) slave2: mysql> select * from db01.table03; +------+------+ | id | name | +------+------+ | 1 | haha | | 2 | wowo | | 4 | yoyo | | 1 | haha | | 2 | wowo | | 4 | yoyo | +------+------+ 6 rows in set (0.00 sec)
總結:
測試驗證在master上面操做,會同步到slave1上面,相對slave2來講,也等於在slave1上面進行了相同的操做,全部slave2上面也會發生相應的改變