1. 程式人生 > >mysql 復制參數replicate_do_db和replicate_ignore_db介紹

mysql 復制參數replicate_do_db和replicate_ignore_db介紹

slave rec 新的 mysq 就是 tar 5.1 並且 復制配置

演示環境:

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 s刪除是,這樣的話master上對mysql庫的操作是不會同步到slave上的mysql庫中的。

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

mysql 復制參數replicate_do_db和replicate_ignore_db介紹