mysql 复制参数replicate_do_db和replicate_ignore_db介绍

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 复制参数replicate_do_db和replicate_ignore_db介绍相关的知识,希望对你有一定的参考价值。

演示环境:

1.mysql5.7.24 二进制安装包
2.master库和slave库都是全新的环境进行配置
3.MySQL开启gtid 并且指定具体的数据库进行同步库

maser 库my.cnf配置操作:

[[email protected]_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 [email protected]‘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配置操作:

[[email protected] ~]# 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                             
[[email protected] ~]# 

##slave库上配置如下参数指定数据库进行同步,master库上不需要配置如下参数:

[[email protected] ~]# 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服务:

[[email protected] ~]# gzip -d juwo_2018-12-15.sql.gz 
[[email protected] ~]# ll juwo_2018-12-15.sql 
-rw-r--r-- 1 root root 3936 12月 15 15:57 juwo_2018-12-15.sql
[r[email protected] ~]# 

####导入数据到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 
statusG" |grep -i "yes"

到此处主从复制配置完成

参数replicate_do_db和replicate_ignore_db演示分析:

master库上操作:

[email protected] [mysql]>use mysql
Database changed

[email protected] [mysql]>grant all on test01.* to [email protected]‘127.0.0.1‘ identified by ‘356893‘;

[email protected] [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库上操作:

[email protected] [test01]>select user,host from mysql.user where user=‘test01‘;
Empty set (0.00 sec)

[email protected] [test01]>

空的没有被同步过来

master上操作:

[email protected] [mysql]>use test01;
Database changed

[email protected] [test01]>grant all on test01.* to [email protected]‘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库上操作:

[email protected] [test01]>drop user [email protected]‘127.0.0.1‘;
Query OK, 0 rows affected (0.00 sec)
[email protected] [test01]>select user,host from mysql.user where user=‘test02‘;
Empty set (0.00 sec)
[email protected] [test01]>

slave库上操作:看到slave库上的test02用户同时也被删除了

[email protected] [test01]>select user,host from mysql.user where user=‘test02‘;
Empty set (0.00 sec)

在master上操作删除一开始use切入mysql库创建的用户[email protected]‘127.0.0.1‘

[email protected] [test01]>drop user [email protected]‘127.0.0.1‘;
Query OK, 0 rows affected (0.01 sec)

在slave库上查看:同步报错
原因是:一开始use切入mysql库创建的用户[email protected]‘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库上把这个用户创建出来:

[email protected] [test01]>grant  all on test01.* to [email protected]‘127.0.0.1‘ identified by ‘123456‘;
Query OK, 0 rows affected, 1 warning (0.01 sec)

**重启slave  sql_thread 线程**
[email protected] [test01]>stop slave sql_thread;
[email protected] [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 s删除是,这样的话master上对mysql库的操作是不会同步到slave上的mysql库中的。

稍微有点绕,但是亲自测试下,就明白怎么回事了。

以上是关于mysql 复制参数replicate_do_db和replicate_ignore_db介绍的主要内容,如果未能解决你的问题,请参考以下文章

mysql中replicate_wild_do_table和replicate_do_db区别

MySQL 主从复制遇到的问题总结

在线业务mysql主从数据库同步不一致问题解决

MySQL主从复制参数注解

mysql复制参数讲解

mysql习惯及主从复制参数设置