MySQL 5.7版本支持多源復(fù)制,MySQL 5.5和5.6不支持。
多源復(fù)制可以讓多個(gè)主節(jié)點(diǎn)同時(shí)并行進(jìn)行復(fù)制到一個(gè)從節(jié)點(diǎn)上。一個(gè)slave為每個(gè)master創(chuàng)建一個(gè)復(fù)制通道。
至少需要兩臺(tái)主庫和一臺(tái)從庫。
多源復(fù)制中的主庫,可以配置成基于全局事務(wù)標(biāo)準(zhǔn)(GTID)的復(fù)制,或者基于二進(jìn)制日志的復(fù)制。
IP規(guī)劃
主庫01192.168.174.201
主庫02192.168.174.202
從庫192.168.174.203
1.在多源復(fù)制的從庫中,需要基于表的repositories,和基于文件的repositories不兼容。
在從庫上面操作
可以將下面參數(shù)添加到參數(shù)文件中
master-info-repository=TABLE
relay-log-info-repository=TABLE
master_info_repository
決定包含master狀態(tài)和連接信息的slave日志,是以文件格式(master.info),還是以表格式(mysql.slave_master_info)存在。
當(dāng)沒有復(fù)制線程執(zhí)行的時(shí)候,可以改變這個(gè)參數(shù)的值。
這個(gè)參數(shù)還會(huì)對(duì)sync_master_info系統(tǒng)參數(shù)有直接的影響。
relay_log_info_repository
這個(gè)參數(shù)決定寫到文件(relay-log.info)或表(mysql.slave_relay_log_info)中的中繼日志slave節(jié)點(diǎn)的位置。只有當(dāng)沒有復(fù)制線程執(zhí)行時(shí),才可修改這個(gè)參數(shù)的值。
這個(gè)參數(shù)用于存放中繼日志的信息。默認(rèn)是文件格式,文件的默認(rèn)名是relay-log.info。
如果是TABLE格式,日志信息會(huì)寫到mysql.slave_relay_log_info。
動(dòng)態(tài)修改,使用下面命令
STOP SLAVE;
SET GLOBAL master_info_repository = 'TABLE';
STOP SLAVE;
SET GLOBAL relay_log_info_repository = 'TABLE';
mysql> show global variables like '%repositor%';
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| master_info_repository | FILE |
| relay_log_info_repository | FILE |
+---------------------------+-------+
2 rows in set (0.03 sec)
mysql> SET GLOBAL master_info_repository = 'TABLE';
Query OK, 0 rows affected (0.00 sec)
mysql> SET GLOBAL relay_log_info_repository = 'TABLE';
ERROR 1766 (HY000): Unknown error 1766
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show global variables like '%repositor%';
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| master_info_repository | TABLE |
| relay_log_info_repository | TABLE |
+---------------------------+-------+
2 rows in set (0.01 sec)
編輯從庫的其他配置文件
[root@localhost 5505]# vim /mysql_data/cnf/my.cnf
# Log
server-id = 300
log-bin = /mysql_log/binlog/mysql-bin
relay-log = /mysql_log/binlog/product-relay-bin
relay-log-index = /mysql_log/binlog/product-relay-index
之后重啟數(shù)據(jù)庫,使得參數(shù)生效
2. 修改主庫1和主庫2參數(shù)文件,創(chuàng)建復(fù)制用戶,創(chuàng)建測試數(shù)據(jù)
編輯主庫的配置文件,注意,每個(gè)庫的server-id不能相同
[root@localhost install]# vim /etc/my.cnf
# Log
server-id = 100
log-bin = /log/binlog/mysql-bin
之后重啟數(shù)據(jù)庫,使得參數(shù)生效
創(chuàng)建復(fù)制用戶
mysql> grant replication slave on *.* to 'repl'@'192.168.174.%' identified by 'repl';
Query OK, 0 rows affected, 1 warning (0.06 sec)
mysql> select version();
+---------------+
| version() |
+---------------+
| 5.7.17-11-log |
+---------------+
1 row in set (0.00 sec)
創(chuàng)建測試數(shù)據(jù)
主庫1
mysql> create database sale;
Query OK, 1 row affected (0.06 sec)
mysql> use sale;
Database changed
mysql> create table sale_record(id int);
Query OK, 0 rows affected (0.16 sec)
mysql> insert into sale_record values(10),(20);
Query OK, 2 rows affected (0.95 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> commit;
Query OK, 0 rows affected (0.01 sec)
主庫2
mysql> use market;
Database changed
mysql> create table market_record(id int)
-> ;
Query OK, 0 rows affected (0.04 sec)
mysql> insert into market_record values (100),(200);
Query OK, 2 rows affected (0.25 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> commit;
Query OK, 0 rows affected (0.01 sec)
3. 備份主庫1和主庫2
主庫1
[root@MySQL01 mysql_software_57]# bin/mysqldump -uroot -p'root' -h 127.0.0.1 -q --single-transaction --master-data=2 -B sale > /tmp/20171211_sale.sql
主庫2
[root@localhost mysql_software_57]# bin/mysqldump -uroot -p'root' -S /mysql_data_57/mysql.sock -q --single-transaction --master-data=2 -B market > /tmp/20171211_market.sql
將主庫1和主庫2上面的備份文件拷貝到從庫上面,進(jìn)行恢復(fù)
在從庫上面操作
[root@MySQL03 mysql_software_57]# bin/mysql -uroot -p'root' -h 127.0.0.1 < /tmp/20171211_sale.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@MySQL03 mysql_software_57]# bin/mysql -uroot -p'root' -h 127.0.0.1 < /tmp/20171211_market.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
4. 搭建多源復(fù)制
搭建到主庫1的復(fù)制,將通道起名為master-1
mysql> change master to
-> master_host='192.168.174.201',
-> master_port=3306,
-> master_user='repl',
-> master_password='repl',
-> master_log_file='mysql-bin.000017',
-> master_log_pos=1209
-> FOR CHANNEL 'master-1';
Query OK, 0 rows affected, 1 warning (0.04 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.174.201
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000017
Read_Master_Log_Pos: 1209
Relay_Log_File: mysqld-relay-bin-master@002d1.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000017
Slave_IO_Running: No
Slave_SQL_Running: No
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: 1209
Relay_Log_Space: 154
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: NULL
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: 0
Master_UUID:
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name: master-1
Master_TLS_Version:
1 row in set (0.00 sec)
啟動(dòng)通道m(xù)aster-1的IO和SQL線程
mysql> START SLAVE IO_THREAD FOR CHANNEL 'master-1';
Query OK, 0 rows affected (0.00 sec)
mysql> START SLAVE SQL_THREAD FOR CHANNEL 'master-1';
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.174.201
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000017
Read_Master_Log_Pos: 1209
Relay_Log_File: mysqld-relay-bin-master@002d1.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000017
Slave_IO_Running: Yes
Slave_SQL_Running: 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: 1209
Relay_Log_Space: 541
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
Master_UUID: 203fe772-177e-11e7-b15c-000c296b3b20
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name: master-1
Master_TLS_Version:
1 row in set (0.00 sec)
啟動(dòng)通道m(xù)aster-2的IO線程和SQL線程
mysql> change master to
-> master_host='192.168.174.202',
-> master_port=3306,
-> master_user='repl',
-> master_password='repl',
-> master_log_file='mysql-bin.000014',
-> master_log_pos=1239
-> FOR CHANNEL 'master-2';
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> START SLAVE IO_THREAD FOR CHANNEL 'master-2';
Query OK, 0 rows affected (0.00 sec)
mysql> START SLAVE SQL_THREAD FOR CHANNEL 'master-2';
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.174.201
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000017
Read_Master_Log_Pos: 1481
Relay_Log_File: mysqld-relay-bin-master@002d1.000002
Relay_Log_Pos: 592
Relay_Master_Log_File: mysql-bin.000017
Slave_IO_Running: Yes
Slave_SQL_Running: 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: 1481
Relay_Log_Space: 813
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
Master_UUID: 203fe772-177e-11e7-b15c-000c296b3b20
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name: master-1
Master_TLS_Version:
*************************** 2. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.174.202
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000014
Read_Master_Log_Pos: 1239
Relay_Log_File: mysqld-relay-bin-master@002d2.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000014
Slave_IO_Running: Yes
Slave_SQL_Running: 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: 1239
Relay_Log_Space: 541
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: 2
Master_UUID: 2efd664c-177f-11e7-8323-000c29fcf2cd
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name: master-2
Master_TLS_Version:
2 rows in set (0.01 sec)
進(jìn)行測試
在主庫1上面插入數(shù)據(jù)
mysql> insert into sale_record values(30),(40),(50);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> commit;
Query OK, 0 rows affected (0.01 sec)
在主庫2上面插入數(shù)據(jù)
mysql> insert into market_record values(300),(400),(500);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> commit;
Query OK, 0 rows affected (0.01 sec)
在從庫上面進(jìn)行查詢
mysql> select * from sale.sale_record;
+------+
| id |
+------+
| 10 |
| 20 |
| 30 |
| 40 |
| 50 |
+------+
5 rows in set (0.00 sec)
mysql> select * from market.market_record;
+------+
| id |
+------+
| 100 |
| 200 |
| 300 |
| 400 |
| 500 |
+------+
5 rows in set (0.00 sec)
常用命令
啟動(dòng)多源復(fù)制
啟動(dòng)所有配置的復(fù)制通道的IO線程
mysql> start slave io_thread;
Query OK, 0 rows affected (0.01 sec)
啟動(dòng)所有配置的復(fù)制通道的SQL線程
mysql> start slave sql_thread;
Query OK, 0 rows affected (0.01 sec)
啟動(dòng)指定的復(fù)制通道
mysql> start slave io_thread for channel 'master-1';
Query OK, 0 rows affected (0.00 sec)
停止多源復(fù)制
停止所有配置的復(fù)制通道的IO線程
mysql> stop slave io_thread;
Query OK, 0 rows affected (0.01 sec)
停止所有配置的復(fù)制通道的SQL線程
mysql> stop slave sql_thread;
Query OK, 0 rows affected (0.00 sec)
停止指定的復(fù)制通道
mysql> stop slave sql_thread for channel 'master-1';
Query OK, 0 rows affected, 1 warning (0.00 sec)
重置多源復(fù)制的從庫
重置所有配置的復(fù)制通道
RESET SLAVE;
重置指定的通道
RESET SLAVE FOR CHANNEL 'master-1';
分享文章:MySQL5.7搭建多源復(fù)制
本文地址:
http://weahome.cn/article/jjpesj.html