MySQL級聯復制
阿新 • • 發佈:2019-05-08
root tables send info update one plus mysq mysql-bin MySQL級聯復制
在生產換進中有一種主從復制的方法主節點先將數據同步到一個中間的從節點,然後由從節點給後續的其他從節點來復制數據,這種復制方式稱為級聯復制。
級聯復制的好處是可以極大的減輕主節點的壓力
級聯復制在配置時需要在中間節點上啟用log_slave_updates的選項。
級聯復制的配置方法
準備主機4臺,1臺主節點(Master),1臺中間從節點(Slave),1臺從節點(Slave1)
主機 | 系統 | ip |
---|---|---|
Master | CentOS7 | 192.168.73.110 |
Slave | CentOS7 | 192.168.73.111 |
Slave1 | CentOS7 | 192.168.73.112 |
主節點配置(Master)
1.修改配置文件
[mysqld]
log-bin=/data/bin/mysql-bin #啟動二進制日誌
binlog-format=row #修改日誌格式
server-id=1
2.創建二級制日誌目錄
[[email protected] ~]# mkdir /data/bin
[[email protected] ~]# chown -R mysql.mysql /data/bin
3.啟動服務,查看當前二進制日誌所在的位置
[[email protected] ~]# systemctl start mariadb [[email protected] ~]# mysql -e "SHOW MASTER LOGS;" +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 26753 | | mysql-bin.000002 | 921736 | | mysql-bin.000003 | 245 | #記錄當前二進制的位置 +------------------+-----------+
4.創建一個用來復制數據的賬戶
[[email protected] ~]# mysql -e "GRANT REPLICATION SLAVE ON *.* TO ‘repluser‘@‘192.168.73.%‘ IDENTIFIED BY ‘centos‘;"
中間從節點配置
1.修改配置文件
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
log-bin=/data/bin/mysql-bin
binlog-format=row
read-only
log_slave_updates
server-id=2
2.創建二進制日誌目錄
[[email protected] ~]# mkdir /data/bin
[[email protected] ~]# chown -R mysql.mysql /data/bin
3.啟動MySQL服務
[[email protected] ~]# systemctl start mariadb
4.寫入CHANGE MASTER TO信息
CHANGE MASTER TO
MASTER_HOST=‘192.168.73.110‘,
MASTER_USER=‘repluser‘,
MASTER_PASSWORD=‘centos‘,
MASTER_PORT=3306,
MASTER_LOG_FILE=‘mysql-bin.000003‘,
MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.01 sec)
5.查看下從節點的配置狀態
MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.73.110
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 245
Relay_Log_File: mariadb-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: No #線程尚未開啟
Slave_SQL_Running: No #線程尚未開啟
6.啟動線程
MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.00 sec)
7.再次查看從節點狀態
MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.73.110
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 402 #已經有小部分數據被復制過來
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 686
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes #線程已經啟動
Slave_SQL_Running: Yes #線程已經啟動
8.測試
8.1在主節點導入數據庫
[[email protected] ~]# mysql < hellodb_innodb.sql
[[email protected] ~]# mysql -e "SHOW DATABASES;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
8.2從節點查看是否已經同步
[[email protected] ~]# mysql -e "SHOW DATABASES;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
配置Slave1
由於此時各節點上已經有數據,作為後來追加的從服務器,首先需要將之前的所有數據使用備份恢復一次然後再進行主從復制進行同步
1.在中間節點上將數據庫備份出來,並將數據傳送到後續的Slave1主機上
[[email protected] ~]# mysqldump -A --single-transaction -F --master-data=1 > /data/all.sql
[[email protected] ~]# scp /data/all.sql 192.168.73.112:/data
2.在Slave1主機上修改配置文件
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
read-only
server-id=3
3.啟動MySQL服務
[[email protected] ~]# systemctl start mariadb
4.對備份文件做修改
找到CHANGE MASTER TO行對信息加以修改
[[email protected] ~]# vim /data/all.sql
CHANGE MASTER TO MASTER_HOST=‘192.168.73.111‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘centos‘,MASTER_PORT=3306,MASTER_LOG_FILE=‘mysql-bin.000004‘, MASTER_LOG_POS=245;
5.將修改後的備份文件導入數據庫
[[email protected] ~]# mysql < /data/all.sql
6.查看下slave status
[[email protected] ~]# mysql -e "SHOW SLAVE STATUS\G;"
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.73.111
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 245
Relay_Log_File: mariadb-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: No
Slave_SQL_Running: No
7.啟動線程
[[email protected] ~]# mysql -e "START SLAVE;"
8.再次查看slave status
[[email protected] ~]# mysql -e "show slave status\G;"
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.73.111
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 245
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes #線程已經全部啟動
Slave_SQL_Running: Yes
測試
在主節點上刪除hellodb庫中的teachers表
[[email protected] ~]# mysql -e "SHOW TABLES FROM hellodb;" #先查看下庫是否有teachers表
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
[[email protected] ~]# mysql -e "DROP TABLE hellodb.teachers;" #刪表
[[email protected] ~]# mysql -e "SHOW TABLES FROM hellodb;" #再次確認表是否刪除
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| toc |
+-------------------+
在slave1節點上查看是否同步
[[email protected] ~]# mysql -e "SHOW TABLES FROM hellodb;" #查看從節點是否還存在teachers表
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| toc |
+-------------------+
級聯復制配置成功
MySQL級聯復制