概述:本质上和传统异步复制没什么区别,就是加了GTID参数。
且可以用传统的方式来配置主从,也可以用GTID的方式来自动配置主从。
这里使用GTID的方式来自动适配主从。
一、基于GTID的异步复制(一主一从)
【1】环境
操作系统:CentOS linux 7.5
数据库版本:5.7.26
数据库架构:主从复制,主库用于生产,从库用于数据容灾和主库备机,采用默认传统的异步复制。
主库IP:192.168.135.158 端口:3306
从库IP:192.168.135.159 端口:3306
【2】my.cnf 参数配置(主从都配)
#replication_new log_bin=/mysql/log/3306/mysql-bin #开启binlog log_bin_index=/mysql/log/3306/mysql-bin.index binlog_format=row binlog_rows_query_log_events=on max_binlog_size=2048 bind-address=0.0.0.0 server_id=2013306 #注意,这里从库的server_id和主库一定要不一样 expire_logs_days=7 #超过7天的binlog清理 innodb_support_xa=1 binlog_cache_size=1M log_bin_trust_function_creators=1 #同步存储过程、函数、触发器 innodb_flush_log_at_trx_commit=1 sync_binlog=1 transaction-isolation=read-committed #slave replication relay_log=/mysql/log/3306/relaylog/mysql-relay.log log-slave-updates=1 read_only=1 slave-parallel-type=LOGICAL_CLOCK slave-parallel-workers=4 master_info_repository=table #master_info 会记录到 mysql.slave_master_info relay_log_info_repository=table #relay_log 会记录到,mysql.slave_relay_log_info relay_log_recovery=1 slave_skip_errors=ddl_exist_errors slave_preserve_commit_order=1 #增加的GTID参数 gtid_mode=on enforce_gtid_consistency=1 #on:当发现语句/事务不支持GTID时,返回错误信息 log-slave-updates=1 binlog_gtid_simple-recovery=1 #5.7.6以下默认为off,5.7.6以上默认为on
【3】在主库创建复制用户
这里也会记录到binlog,开启主从同步后会一并复制过去,以便我们做主从且换
create user 'rpl'@'192.168.135.%' identified by '123456'; grant replication slave on *.* to 'rpl'@'192.168.135.%'; flush privileges; select user,host from mysql.user;
【4】在从库上运行
stop slave;change master to master_host='192.168.135.158', master_port=3306, master_user='rpl', master_password='123456', master_auto_position=1; start slave;
【5】核验
【5.1】show slave status\G 查看是否有误

OK。两个线程启动了,Error字样的字段也没有任何问题。
【5.2】核验之前创建的用户是否有同步过来

OK,没有问题。
【5.3】在主库创建测试数据库与测试表查看同步情况
#创建test2库,以及test2.test1表 create database test2; create table test2.test1(id int); insert into test2.test1 values(1);commit; select * from test2.test1;
从库查看
select * from test2.test1;

OK 核验完成。