replicate_wild_do_table和replicate-wild-ignore-table的使用【转】

使用replicate_do_db和replicate_ignore_db时有一个隐患,跨库更新时会出错。

如在Master(主)服务器上设置 replicate_do_db=test(my.conf中设置)
use mysql;
update test.table1 set ......
那么Slave(从)服务器上第二句将不会被执行

 

如Master设置 replicate_ignore_db=mysql
use mysql;
update test.table1 set ......
那么Slave上第二句会被忽略执行

 

原因是设置replicate_do_db或replicate_ignore_db后,MySQL执行sql前检查的是当前默认数据库,所以跨库更新语句在Slave上会被忽略。

可以在Slave上使用 replicate_wild_do_table 和 replicate_wild_ignore_table 来解决跨库更新的问题,如:
replicate_wild_do_table=test.%

replicate_wild_ignore_table=mysql.%


这样就可以避免出现上述问题了

---------------------

本文来自 tlpower 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/tlpower/article/details/7891870?utm_source=copy

mysql主从复制的两个参数

binlog-do-db:指定mysql的binlog日志记录哪个db

实验:
主库:
binlog-do-db=test
binlog-do-db=xiaobin
root@[mysql]>show variables like '%binlog_format';
+---------------+-----------+
| Variable_name | Value 

    |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
1 row in set (0.00 sec)

root@[mysql]>use mysql;
Database changed

root@[mysql]>create table test.dd (id int);
Query OK, 0 rows affected (0.00 sec)

root@[mysql]>select * from test.dd;
Empty set (0.02 sec)


从库:
(testing)root@localhost [test]> use test;
Database changed

(testing)root@localhost [test]> show tables;
Empty set (0.01 sec)

----------------------------------------------

主库:
root@[mysql]>use xiaobin;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
root@[xiaobin]>create table test.dd (id int);
Query OK, 0 rows affected (0.02 sec)

从库:
(testing)root@localhost [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| dd             |
+----------------+
1 row in set (0.00 sec)

结论:在binlog_format=STATEMENT时,在用use dbname的格式下,如果dbname没有在binlog-do-db里,DDL和DML语句都不会被记录在binlog里。即使指定具体的test.dd;

————————————————————————————————————————

主库:
root@[(none)]>show variables like '%binlog_format';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+
1 row in set (0.00 sec)

root@[(none)]>use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
root@[mysql]>create table test.dd (id int);
Query OK, 0 rows affected (0.01 sec)

从库:
(testing)root@localhost [test]> show tables;
Empty set (0.02 sec)
---------

主库:
root@[mysql]>insert into test.dd values(11);
Query OK, 1 row affected (0.00 sec)

root@[mysql]>commit;
Query OK, 0 rows affected (0.01 sec)

从库:
(testing)root@localhost [test]> select * from dd;
+------+
| id   |
+------+
|   11 |
+------+
1 row in set (0.00 sec)

结论:在row模式下,在用use dbname的格式下,如果dbname没有在binlog-do-db里,DDL语句都不会被记录在binlog里。即使指定具体的test.dd;DML语句会记录。

————————————————————————————————————————————

主库:
root@[(none)]>show variables like '%binlog_format';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | MIXED |
+---------------+-------+
1 row in set (0.00 sec)

root@[(none)]>use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
root@[mysql]>create table test.dd (id int);
Query OK, 0 rows affected (0.01 sec)

从库:
(testing)root@localhost [test]> show tables;
Empty set (0.02 sec)
---------
主库:
root@[mysql]>insert into test.dd values(11);
Query OK, 1 row affected (0.00 sec)

root@[mysql]>commit;
Query OK, 0 rows affected (0.01 sec)

从库:
(testing)root@localhost [test]> select * from dd;
Empty set (0.01 sec)

结论:在mixed模式下,在用use dbname的格式下,如果dbname没有在binlog-do-db里,DDL、DML语句都不会被记录在binlog里。即使指定具体的test.dd;

总结:
在有几个数据库的情况下
binlog-do-db=db1

use db1;
update db1.table1 set col1=10,db2.table2 set col2=10;
当使用statement模式时,两个修改都会被记录到binlog里;当使用row模式时,只有table1的修改会记录到binlog,table2不会记录。

use db4;
update db1.table1 set col1=10,db2.table2 set col2=10;
当使用statement模式时,两个修改都不会被记录到binlog里;当使用row模式时,只有table1的修改会记录到binlog,table2不会记录。




Replicate_Do_DB:参数是在slave上配置,指定slave要复制哪个库

Replicate-Do-DB=sales

use price;
update sales.february set amount=amount+100
当使用statement模式时,update语句将不会被复制到slave上;当使用row模式时,update语句会复制到slave上;


Replicate-Do-DB=db1

use db1;
update db1.table1 set col1=10,db2.table2 set col2=10;
当使用statement模式时,两个修改都会复制到slave上;当使用row模式时,只有table1的update语句会复制到slave上,table2不会复制。

use db4;
update db1.table1 set col1=10,db2.table2 set col2=10;
当使用statement模式时,两个修改都不会复制到slave上;当使用row模式时,只有table1的update语句会复制到slave上,table2不会复制。


建议在没有完全测试清楚的情况下,mysql复制的这几个选择性参数要慎用,因为在binlog_format不同的情况下,会对binlog产生不同的影响,从而可能导致主从数据不一致。
如果有需要,可以使用replicate-wild-do-table和Replicate-Ignore-Table代替。

转自

mysql参数:binlog-do-db和replicate-do-db_小濱_新浪博客 http://blog.sina.com.cn/s/blog_747f4c1d0102w9pp.html

原文地址:https://www.cnblogs.com/paul8339/p/9687909.html