1. 程式人生 > >mysql 複製引數replicate_do_db和replicate_ignore_db介紹

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
[[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 
status\G" |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庫中的。

稍微有點繞,但是親自測試下,就明白怎麼回事了。