1.复制过滤参数介绍
首先我们要了解设置复制过滤的不同参数。复制过滤是在从库端设置的,可以只复制某些库或某些表,也可以忽略复制某些库或某些表。这些都是由不同参数控制的,下面简单介绍下不同参数的作用。
- REPLICATE_DO_DB:指定只同步某个库的数据
- REPLICATE_IGNORE_DB:忽略某个库的同步
- REPLICATE_DO_TABLE:指定同步某个表
- REPLICATE_IGNORE_TABLE:忽略某个表的同步
- REPLICATE_WILD_DO_TABLE:指定同步某些表,可以用通配符
- REPLICATE_WILD_IGNORE_TABLE:忽略某些表的同步,可以用通配符
- REPLICATE_REWRITE_DB:从库端替换库名
这些复制过滤参数还是很好理解的,只看名字就能大概了解该参数的作用。默认情况下,这些参数是都没有设置的,开启主从复制后从库端会默认同步全部从主库发来的数据。
2.修改复制过滤选项
当我们想临时调整从库的复制策略时,可以设置上述参数。我们可以将过滤参数写入配置文件然后重启从库即可应用,但这种方法需要重启实例,不做推荐。MySQL5.7版本可以进行在线设置复制过滤了。但是还是得停复制,不过不用重启实例了,方便进行临时性的调整。主要用到的是CHANGE REPLICATION FILTER语句,下面就简单的测试一下:
# 默认未设置复制过滤 mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.3.16 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000004 Read_Master_Log_Pos: 35198 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 910 Relay_Master_Log_File: binlog.000004 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 35198 # 设置忽略db1库的复制 mysql> STOP SLAVE SQL_THREAD; Query OK, 0 rows affected (0.00 sec) mysql> CHANGE REPLICATION FILTER REPLICATE_IGNORE_DB = (db1); Query OK, 0 rows affected (0.00 sec) mysql> START SLAVE SQL_THREAD; Query OK, 0 rows affected (0.01 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.3.16 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000004 Read_Master_Log_Pos: 35198 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 910 Relay_Master_Log_File: binlog.000004 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: db1 Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 35198 # 主库创建db1测试从库是否同步 mysql> CREATE DATABASE `db1` DEFAULT CHARACTER SET utf8; Query OK, 1 row affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db1 | | mysql | | performance_schema | | sys | | testdb | +--------------------+ 6 rows in set (0.00 sec) # 查看从库状态 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | testdb | +--------------------+ 5 rows in set (0.00 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.3.16 Master_User: repl Master_Port: 33061 Connect_Retry: 60 Master_Log_File: binlog.000004 Read_Master_Log_Pos: 35383 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 1095 Relay_Master_Log_File: binlog.000004 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: db1 Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 35383 # 取消复制过滤参数 mysql> STOP SLAVE SQL_THREAD; Query OK, 0 rows affected (0.01 sec) mysql> CHANGE REPLICATION FILTER REPLICATE_IGNORE_DB = (); Query OK, 0 rows affected (0.00 sec) mysql> START SLAVE SQL_THREAD; Query OK, 0 rows affected (0.01 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.3.16 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000004 Read_Master_Log_Pos: 35383 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 1095 Relay_Master_Log_File: binlog.000004 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 35383
上面我们简单演示了下使用CHANGE REPLICATION FILTER语句在线修改复制过滤选项的方法,列举的那些过滤参数都可以使用该语句修改,不过要注意有些选项是互斥的。应该按照实际需求去设置合适的参数,下面给出官方文档中的示范语法:
CHANGE REPLICATION FILTER filter[, filter][, ...] filter: REPLICATE_DO_DB = (db_list) | REPLICATE_IGNORE_DB = (db_list) | REPLICATE_DO_TABLE = (tbl_list) | REPLICATE_IGNORE_TABLE = (tbl_list) | REPLICATE_WILD_DO_TABLE = (wild_tbl_list) | REPLICATE_WILD_IGNORE_TABLE = (wild_tbl_list) | REPLICATE_REWRITE_DB = (db_pair_list) db_list: db_name[, db_name][, ...] tbl_list: db_name.table_name[, db_table_name][, ...] wild_tbl_list: 'db_pattern.table_pattern'[, 'db_pattern.table_pattern'][, ...] db_pair_list: (db_pair)[, (db_pair)][, ...] db_pair: from_db, to_db
总结:
本篇文章介绍了如何在线更改复制过滤选项的方法,不同的过滤参数有不同的用途,如果你确实有需求要设置过滤参数,建议一定要进行全面测试,某些参数设置后可能影响到其他库表的复制。如果想永久生效,可以在线修改后再加入配置文件内,这样从库重启后还是生效的。