使用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