MySQL多源复制
MySQL多源复制
MySQL多源复制使slave能够同时从多个源master接收事务。 多源复制可用于将多个服务器备份到单个服务器,合并表分片,以及将来自多个服务器的数据合并到单个服务器。 应用事务时,多源复制不会实现任何冲突检测或解决,如果需要,这些任务将留给应用程序。 在多源复制拓扑中,slave服务器应从每个接收事务的主服务器创建复制通道。 以下部分介绍如何设置多源复制。
1. 配置多源复制
介绍如何配置多源复制拓扑,并提供有关配置master和slave的详细信息。这种拓扑需要至少两个master设备和一个slave设备配置。
1.1 配置环境如下
类型 | IP | 端口号 | server-id | 复制通道 |
---|---|---|---|---|
master | 192.168.56.100 | 3306 | 1003306 | master1 |
master | 192.168.56.100 | 3307 | 1003307 | master2 |
slave | 192.168.56.200 | 3308 | 2003308 |
1.2 从库的重要参数配置
在多源复制中,slave需要基于TABLE的存储库。 多源复制与基于FILE的存储库不兼容。mysqld使用的存储库类型可以在启动时配置,也可以动态配置。
- 启动时配置my.cnf
master-info-repository=TABLE relay-log-info-repository=TABLE
- 在线动态配置
STOP SLAVE; SET GLOBAL master_info_repository = 'TABLE'; SET GLOBAL relay_log_info_repository = 'TABLE';
1.3 在Master上导出需要同步的数据库
利用mysqldump进行备份及恢复,mysqldump的使用方法见博客MySQL备份恢复
- 在master1上
mysqldump -S /tmp/mysql3306.sock --master-data=2 --single-transaction --add-drop-database --databases employees > /tmp/empdb.sql
- 在master2上
mysqldump -S /tmp/mysql3307.sock --master-data=2 --single-transaction --add-drop-database --databases test > /tmp/testdb.sql
- 把备份传输到slave上
scp /tmp/empdb.sql /tmp/testdb.sql mysqldb2:/data/backup/
1.4 在master上创建复制账号
-
在master1上
mysql> create user 'repl'@'%' identified by 'wanbin'; mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
-
在master2上
mysql> create user 'repl'@'%' identified by 'wanbin'; mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
1.5 备份数据导入
mysql -S /tmp/mysql3308.sock < empdb.sql
ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.
#出现错误
#解决方法:
mysql> reset master;
mysql -S /tmp/mysql3308.sock < empdb.sql
mysql -S /tmp/mysql3308.sock < testdb.sql
1.6 查看备份文件中Master1与Master2的binlog Pos位置
cat empdb.sql |grep "CHANGE MASTER"
-- CHANGE MASTER TO MASTER_LOG_FILE='my3306_binlog.000004', MASTER_LOG_POS=194;
cat testdb.sql |grep "CHANGE MASTER"
-- CHANGE MASTER TO MASTER_LOG_FILE='my3307_binlog.000011', MASTER_LOG_POS=194;
1.7 Slave进行change master操作
#创建复制账号
create user 'repl'@'%' identified by 'wanbin';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
#change master
CHANGE MASTER TO
MASTER_HOST='192.168.56.100',
MASTER_USER='repl',
MASTER_PASSWORD='wanbin',
MASTER_PORT=3306,
MASTER_LOG_FILE='my3306_binlog.000004',
MASTER_LOG_POS=194
FOR CHANNEL 'master1';
CHANGE MASTER TO
MASTER_HOST='192.168.56.100',
MASTER_USER='repl',
MASTER_PASSWORD='wanbin',
MASTER_PORT=3307,
MASTER_LOG_FILE='my3307_binlog.000011',
MASTER_LOG_POS=194
FOR CHANNEL 'master2';
1.8 开启主从复制
可以通过start slave
命令开启所有复制,也可以通过 start slave for channel 来分别开启。
mysql> start slave for CHANNEL 'master1';
mysql> start slave for CHANNEL 'master2';
2. 监控多源复制
要监视复制通道的状态,需要以下选项:
使用复制Performance Schema表。 这些表的第一列是Channel_Name。 这使您可以基于Channel_Name作为键来编写复杂查询。
使用SHOW SLAVE STATUS FOR CHANNEL Channel_Name
。 默认情况下,如果未使用FOR CHANNEL通道子句,则此语句显示所有通道的从站状态,每个通道一行。 标识符Channel_name将添加为结果集中的列。 如果提供了FOR CHANNEL通道子句,则结果仅显示指定复制通道的状态。
root@localhost [test] 15:46:02>SHOW SLAVE STATUS FOR CHANNEL 'master1'\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.56.100
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: my3306_binlog.000004
Read_Master_Log_Pos: 973
Relay_Log_File: mysqldb2-relay-bin-master1.000002
Relay_Log_Pos: 1103
Relay_Master_Log_File: my3306_binlog.000004
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: 973
Relay_Log_Space: 1321
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: 1003306
Master_UUID: 7390a401-b705-11e8-9ed9-080027b0b461
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: 7390a401-b705-11e8-9ed9-080027b0b461:10-13
Executed_Gtid_Set: 3a068bf8-cdeb-11e8-8176-080027b0b461:1-16,
7390a401-b705-11e8-9ed9-080027b0b461:10-13,
d934e7cb-d1d1-11e8-85ce-0800275b8a9a:1-3
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name: master1
Master_TLS_Version:
1 row in set (0.00 sec)
使用Performance Schema.replication_connection_status
表监控
root@localhost [test] 15:49:21>use performance_schema
Database changed
root@localhost [performance_schema] 15:49:43>select * from replication_connection_status\G
*************************** 1. row ***************************
CHANNEL_NAME: master1
GROUP_NAME:
SOURCE_UUID: 7390a401-b705-11e8-9ed9-080027b0b461
THREAD_ID: 44
SERVICE_STATE: ON
COUNT_RECEIVED_HEARTBEATS: 64
LAST_HEARTBEAT_TIMESTAMP: 2018-10-17 15:49:41
RECEIVED_TRANSACTION_SET: 7390a401-b705-11e8-9ed9-080027b0b461:10-13
LAST_ERROR_NUMBER: 0
LAST_ERROR_MESSAGE:
LAST_ERROR_TIMESTAMP: 0000-00-00 00:00:00
*************************** 2. row ***************************
CHANNEL_NAME: master2
GROUP_NAME:
SOURCE_UUID: 3a068bf8-cdeb-11e8-8176-080027b0b461
THREAD_ID: 47
SERVICE_STATE: ON
COUNT_RECEIVED_HEARTBEATS: 58
LAST_HEARTBEAT_TIMESTAMP: 2018-10-17 15:49:23
RECEIVED_TRANSACTION_SET: 3a068bf8-cdeb-11e8-8176-080027b0b461:14-16
LAST_ERROR_NUMBER: 0
LAST_ERROR_MESSAGE:
LAST_ERROR_TIMESTAMP: 0000-00-00 00:00:00
2 rows in set (0.00 sec)