MySQL5.7在主从复制上面相对之前版本多了一些新特性,包括多源复制、基于组提交的并行复制、在线修改Replication Filter、GTID增强、半同步复制增强等。
多源复制:多源复制加入了一个叫做Channel的概念, 每一个Channel都是一个独立的Slave,都有一个IO_THREAD和SQL_THREAD。原理和普通复制一样。我们只需要对每一个Master执行Change Master 语句,只需要在每个语句最后使用For Channel来进行区分。由于复制的原理没有改变,在没有开启GTID的时候Master的版本可以是MySQL5.5、5.6、5.7。并且从库需要master-info-repository、relay-log-info-repository设置为table,否则会报错:
ERROR 3077 (HY000): To have multiple channels, repository cannot be of type FILE; Please check the repository configuration and convert them to TABLE.
这里需要注意:从的版本若是5.7.x~5.7.13,主的版本不能是MySQL5.5,因为MySQL5.5没有server_uuid函数。该问题在MySQL5.7.13里修复(Bug #22748612)。
部署三个MySQL实例
MySQL5.5镜像:https://www.cnblogs.com/wshenjin/p/9585696.html
MySQL5.7镜像:https://www.cnblogs.com/wshenjin/p/10100949.html
环境
[root@Docker_Machine_192.168.31.130 ~]# mkdir -p mysql5.5_master1/data mysql5.5_master2/data mysql5.7_slave/data
[root@Docker_Machine_192.168.31.130 ~]# chown mysql:mysql -R mysql5.5_master* mysql5.7_slave1
slave的配置文件:
server-id=3
#rep
log_slave_updates = 1
master_info_repository=TABLE
relay_log_info_repository=TABLE
replicate_wild_ignore_table=mysql.%
不管是使用基于二进制日志的复制或者基于事务的复制,要开启多源复制功能,必须需要在从库上设置master_info_repository和relay_log_info_repository两个参数为’TABLE’(在mysql库下可以看见这两个表信息),是用来存储同步信息的,可以设置的值为’FILE’和’TABLE’,默认是FILE。为’FILE’时,会生成master.info 和 relay-log.info两个文件,如果是’TABLE’,信息就会存在mysql.slave_relay_log_info和mysql.slave_master_info两个表中。
GLOBAL master_info_repository、GLOBAL relay_log_info_repository这两个值可以动态调整:
mysql> SET GLOBAL master_info_repository = 'TABLE';
mysql> SET GLOBAL relay_log_info_repository = 'TABLE';
启动两个master实例:
[root@Docker_Machine_192.168.31.130 ~]# docker run -itd -v /data/dokcer/docker_local_volume/mysql5.5_master1/data/:/data/database/mysql/ -v /data/dokcer/docker_local_volume/mysql5.5_master1/my.cnf:/etc/my.cnf --network=net_bridge_192.168.2.0 --ip=192.168.2.101 --name=mysql5.5_master1 volumes/my_percona_server:v5.5.61.2
[root@Docker_Machine_192.168.31.130 ~]# docker run -itd -v /data/dokcer/docker_local_volume/mysql5.5_master2/data/:/data/database/mysql/ -v /data/dokcer/docker_local_volume/mysql5.5_master2/my.cnf:/etc/my.cnf --network=net_bridge_192.168.2.0 --ip=192.168.2.102 --name=mysql5.5_master2 volumes/my_percona_server:v5.5.61.2
启动一个slave实例
[root@Docker_Machine_192.168.31.130 ~]# docker run -itd -v /data/dokcer/docker_local_volume/mysql5.7_slave/data/:/data/database/mysql/ -v /data/dokcer/docker_local_volume/mysql5.7_slave/my.cnf:/etc/my.cnf --network=net_bridge_192.168.2.0 --ip=192.168.2.100 --name=mysql5.7_slave me/my_percona_server:5.7.23.24
建立主从
两个主库上对从库进行主从授权
mysql> grant replication slave , replication client on *.* to slave@192.168.2.100 identified by 'slave#123';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges ;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
从主库上导数据和查看**master status **就忽略了
从库建立主从
mysql> change master to master_host='192.168.2.101', master_port=3306, master_user='slave', master_password='slave#123', master_log_file='mysql-bin.000004', master_log_pos=357 for channel 'master1';
Query OK, 0 rows affected, 2 warnings (0.05 sec)
mysql> change master to master_host='192.168.2.102', master_port=3306, master_user='slave', master_password='slave#123', master_log_file='mysql-bin.000004', master_log_pos=357 for channel 'master2';
Query OK, 0 rows affected, 2 warnings (0.04 sec)
多源复制的相关操作:
查看单个channel的状态:
mysql> show slave status for channel 'master1'\G
停止单个channel的同步:
mysql> stop slave for channel 'master1';
开启单个channel的同步:
mysql> start slave for channel 'master1';
重置单个channel:
mysql> reset slave all for channel 'master1';
查看所有channel:
mysql> show slave status\G
停止所有channel:
mysql> stop slave;
开启所有channel:
mysql> start slave;
监控
监控:系统库performance_schema增加了一些replication的监控表:
mysql> use performance_schema
Database changed
mysql> show tables like 'replicat%';
+-------------------------------------------+
| Tables_in_performance_schema (replicat%) |
+-------------------------------------------+
| replication_applier_configuration |###查看各个channel是否配置了复制延迟
| replication_applier_status |###查看各个channel是否复制正常(service_state)以及事务重连的次数
| replication_applier_status_by_coordinator |###查看各个channel是否复制正常,以及复制错误的code、message和时间
| replication_applier_status_by_worker |###查看各个channel是否复制正常,以及并行复制work号,复制错误的code、SQL和时间
| replication_connection_configuration |###查看各个channel的连接配置信息:host、port、user、auto_position等
| replication_connection_status |###查看各个channel的连接信息
| replication_group_member_stats |###
| replication_group_members |###
+-------------------------------------------+
Replication Filter
在线过滤mysql库:
先关闭sql线程,要是在多源复制中,是关闭所有channel的sql thread。
mysql> stop slave sql_thread;
Query OK, 0 rows affected (0.01 sec)
mysql> CHANGE REPLICATION FILTER REPLICATE_IGNORE_DB=(mysql);
mysql> start slave sql_thread;
Query OK, 0 rows affected (0.04 sec)
若要还原,stop slave sql_thread,然后置空REPLICATE_IGNORE_DB即可。
Replication Filter的语法:
CHANGE REPLICATION FILTER filter[, filter][, ...]
filter:
REPLICATE_DO_DB = (db_list)
| REPLICATE_IGNORE_DB = (db_list)
| REPLICATE_DO_TABLE = (tbl_list)
| REPLICATE_IGNORE_TABLE = (tbl_list)
| REPLICATE_WILD_DO_TABLE = (wild_tbl_list)
| REPLICATE_WILD_IGNORE_TABLE = (wild_tbl_list)
| REPLICATE_REWRITE_DB = (db_pair_list)
db_list:
db_name[, db_name][, ...]
tbl_list:
db_name.table_name[, db_table_name][, ...]
wild_tbl_list:
'db_pattern.table_pattern'[, 'db_pattern.table_pattern'][, ...]
db_pair_list:
(db_pair)[, (db_pair)][, ...]
db_pair:
from_db, to_db
不过这个配置还是要写到配置文件中,不然重启mysqld就没了