演示环境:
1.MySQL5.7.24 二进制安装包
2.master库和slave库都是全新的环境进行配置
3.MySQL开启gtid 并且指定具体的数据库进行同步库
maser 库my.cnf配置操作:
[root@VM_82_178_centos ~]# egrep 'server_id|log_slave_updates|gtid_mode|enforce_gtid_consistency|log_bin|binlog_format' /etc/my.cnf
server_id =1313306
binlog_format =row
log_bin =/data/mysql7/binlog/mysql-bin
log_slave_updates =on
gtid_mode =on
enforce_gtid_consistency =on
mysql -e "grant replication slave on *.* to novelrep@'192.195.1.228' identified by 'JuwoSdk21TbUser'; flush privileges;"
mysqldump -uroot -p'jianwuweirootmysql' --databases test01 test02 --set-gtid-purged=OFF -F --master-data=2 --single-transaction --events|gzip >/opt/juwo_$(date +%F).sql.gz
slave库my.cnf配置操作:
[root@localhost ~]# egrep 'server_id|log_slave_updates|gtid_mode|enforce_gtid_consistency|log_bin|binlog_format' /etc/my.cnf
server_id =2283306
binlog_format =row
log_bin =/data/mysql/binlog/mysql-bin
log_slave_updates =on
gtid_mode =on
enforce_gtid_consistency =on
[root@localhost ~]#
##slave库上配置如下参数指定数据库进行同步,master库上不需要配置如下参数:
[root@localhost ~]# egrep 'replicate_do_db|replicate_ignore_db' /etc/my.cnf
replicate_do_db=test01
replicate_do_db=test02
replicate_ignore_db=test03
replicate_ignore_db=information_schema
replicate_ignore_db=performance_schema
replicate_ignore_db=mysql
replicate_ignore_db=sys
#####重启mysql服务:
[root@localhost ~]# gzip -d juwo_2018-12-15.sql.gz
[root@localhost ~]# ll juwo_2018-12-15.sql
-rw-r--r-- 1 root root 3936 12月 15 15:57 juwo_2018-12-15.sql
[root@localhost ~]#
####导入数据到slave,然后change master to:
mysql -e "source /root/juwo_$(date +%F).sql"
mysql -e "CHANGE MASTER TO MASTER_HOST='192.168.97.131',MASTER_PORT=3306,MASTER_USER='novelrep',MASTER_PASSWORD='JuwoSdk21TbUser',MASTER_AUTO_POSITION = 1;start slave;show slave
status\G" |grep -i "yes"
到此处主从复制配置完成
参数replicate_do_db和replicate_ignore_db演示分析:
master库上操作:
root@localhost [mysql]>use mysql
Database changed
root@localhost [mysql]>grant all on test01.* to test01@'127.0.0.1' identified by '356893';
root@localhost [mysql]>select user,host from mysql.user where user='test01';
+--------+-----------+
| user | host |
+--------+-----------+
| test01 | 127.0.0.1 |
+--------+-----------+
1 row in set (0.00 sec)
slave库上操作:
root@localhost [test01]>select user,host from mysql.user where user='test01';
Empty set (0.00 sec)
root@localhost [test01]>
空的没有被同步过来
master上操作:
root@localhost [mysql]>use test01;
Database changed
root@localhost [test01]>grant all on test01.* to test02@'127.0.0.1' identified by '356893';
Query OK, 0 rows affected, 1 warning (0.01 sec)
slave库上操作:被同步到slave库上了,但是咱们一开始在salve 上是设置的是不同步mysql库的
select user,host from mysql.user where user='test02';
+--------+-----------+
| user | host |
+--------+-----------+
| test02 | 127.0.0.1 |
+--------+-----------+
1 row in set (0.00 sec)
master库上操作:
root@localhost [test01]>drop user test02@'127.0.0.1';
Query OK, 0 rows affected (0.00 sec)
root@localhost [test01]>select user,host from mysql.user where user='test02';
Empty set (0.00 sec)
root@localhost [test01]>
slave库上操作:看到slave库上的test02用户同时也被删除了
root@localhost [test01]>select user,host from mysql.user where user='test02';
Empty set (0.00 sec)
在master上操作删除一开始use切入mysql库创建的用户test01@'127.0.0.1'
root@localhost [test01]>drop user test01@'127.0.0.1';
Query OK, 0 rows affected (0.01 sec)
在slave库上查看:同步报错
原因是:一开始use切入mysql库创建的用户test01@'127.0.0.1' 没有同步到slave库上,如果此时在master库上没有use切入到mysql库来执行删除用户的语句,由于slave上没有同步过来用户,自然master删除用户时,slave库上同步报错
Last_SQL_Errno: 1396
Last_SQL_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: Worker 1 failed executing transaction 'df8f817c-f215-11e8-83e4-525400950067:22' at master log mysql-bin.000001, end_log_pos 7734. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any.
解决办法就是在slave库上把这个用户创建出来:
root@localhost [test01]>grant all on test01.* to test01@'127.0.0.1' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.01 sec)
**重启slave sql_thread 线程**
root@localhost [test01]>stop slave sql_thread;
root@localhost [test01]>start slave sql_thread;
同步就ok了。
由此而得出如下结论:
在salve库上使用replicate_do_db和replicate_ignore_db参数进行过滤数据库同步时,此时在系统默认的mysql库上存在一个隐患,就是在master库上进行跨库update更新mysql库下的表或者是grant 进行mysql授权,以及drop user时,slave库上配置的忽略mysql库同步的参数失效。也就是master库上夸库操作mysql库下的表时,是会同步到slave库的。要是use切换到mysql库进行操作时,grant 进行授权,update 更新mysql库下的表,drop 删除时,这样的话master上对mysql库的操作是不会同步到slave上的mysql库中的。
稍微有点绕,但是亲自测试下,就明白怎么回事了。