在master上,可以使用--binlog-do-db和--binlog-ignore-db来控制二进制日志,进而控制哪个数据库和表被复制,也可以在slave上使用过滤来控制执行的事件。
在slave上,可以根据--replicate-*的选项来控制是否执行来自master上的语句。
控制表级别的复制的选项是--replicate-do-table --replicate-wild-do-table ,--replicate-ignore-table,--replicate-wild-ignore-table
看各个选项的影响
Condition (Types of Options) | Outcome |
---|---|
No --replicate-* options at all: |
The slave executes all events that it receives from the master. |
--replicate-*-db options, but no table options: |
The slave accepts or ignores events using the database options. It executes all events permitted by those options because there are no table restrictions. |
--replicate-*-table options, but no database options: |
All events are accepted at the database-checking stage because there are no database conditions. The slave executes or ignores events based solely on the table options. |
A combination of database and table options: | The slave accepts or ignores events using the database options. Then it evaluates all events permitted by those options according to the table options. This can sometimes lead to results that seem counterintuitive, and that may be different depending on whether you are using statement-based or row-based replication; see the text for an example. |
对于行复制或语句复制来说,达到的效果可能是不同的,下面是官方文档上的例子,所以要设置表级别的过滤的时候,还需要好好测试下。
Suppose that we have two tables mytbl1
in database db1
and mytbl2
in database db2
on the master, and the slave is running with the following options (and no other replication filtering options):
replicate-ignore-db = db1 replicate-do-table = db2.tbl2
Now we execute the following statements on the master:
USE db1; INSERT INTO db2.tbl2 VALUES (1);
The results on the slave vary considerably depending on the binary log format, and may not match initial expectations in either case.
Statement-based replication. The USE
statement causes db1
to be the default database. Thus the --replicate-ignore-db
option matches, and theINSERT
statement is ignored. The table options are not checked.
Row-based replication. The default database has no effect on how the slave reads database options when using row-based replication. Thus, the USE
statement makes no difference in how the --replicate-ignore-db
option is handled: the database specified by this option does not match the database where the INSERT
statement changes data, so the slave proceeds to check the table options. The table specified by --replicate-do-table
matches the table to be updated, and the row is inserted.
将master上的不同数据库分配到不同的slave上,可以使用 --replicate-wild-do-table
这个参数,下面是官网上的例子
master上复制a到1,b到2,c到3,在slave上应该这么设置
Replication slave 1 should use --replicate-wild-do-table=databaseA.%
.
Replication slave 2 should use --replicate-wild-do-table=databaseB.%
.
Replication slave 3 should use --replicate-wild-do-table=databaseC.%
.