MySQL 5.7 has made some progress in configuring replication filtering compared to previous versions. You do not need to restart MySQL, you can directly pass the change replication filter statement.
The syntax of the CHANGE REPLICATION FILTER statement command is as follows:
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
In the slave library, you can use this command to filter the statements of the master library.
--REPLICATE_DO_DB and REPLICATE_IGNORE_DB specify the DB that needs to be synchronized and the DB that does not need to be synchronized, respectively.
-- REPLICATE_DO_TABLE and REPLICATE_IGNORE_TABLE respectively specify the table that needs to be synchronized and the table that does not need to be synchronized. After 5.7.5, the DB name must be specified before the table, such as DB1.tb1_name.
- REPLICATE_WILD_DO_TABLE and REPLICATE_WILD_IGNORE_TABLE can be used to vaguely match table names that need to be synchronized and those that do not need to be synchronized. Single quotes must be used when setting.
--R EPLICATE_REWRITE_DB A set of DBs must be set to replace the corresponding DB operations in the master library with the new DB in the savle library.
In addition to specifying filtering by command, you can also use parameters when mysql starts, such as mysql -uroot -p --replicate_do_db=(db1), which requires restarting the database.
Let's test this function, only the function of replicate_do_db is tested, and the others are similar.
1. By default, all operations of the master will be synchronized to the slave:
master:
mysql> create database db1; Query OK, 1 row affected (0.01 sec) mysql> create database db2; Query OK, 1 row affected (0.00 sec) mysql> create database db3; Query OK, 1 row affected (0.00 sec)
mysql> create table db1.t1 (id int); Query OK, 0 rows affected (0.02 sec) mysql> create table db2.t1 (id int); Query OK, 0 rows affected (0.05 sec)
slave:
mysql> select * from db1.t1; Empty set (0.03 sec) mysql> select * from db2.t1; Empty set (0.00 sec)2. Set REPLICATE_DO_DB and REPLICATE_IGNORE_DB
slave:
mysql> change replication filter replicate_do_db=(db1); ERROR 3017 (HY000): This operation cannot be performed with a running slave sql thread; run STOP SLAVE SQL_THREAD first
#The sql_thread process must be stopped first mysql> stop slave sql_thread; Query OK, 0 rows affected (0.01 sec) mysql> change replication filter replicate_do_db=(db1); Query OK, 0 rows affected (0.00 sec)
mysql> start slave sql_thread; Query OK, 0 rows affected (0.00 sec)
master:
mysql> insert into db1.t1 values(1); Query OK, 1 row affected (0.00 sec) mysql> insert into db2.t1 values(1); Query OK, 1 row affected (0.00 sec) mysql> commit; Query OK, 0 rows affected (0.00 sec)
Only the data of db1 is synchronized in the slave library.
mysql> select * from db1.t1; +------+ | id | +------+ | 1 | +------+ 1 row in set (0.00 sec) mysql> select * from db2.t1; Empty set (0.00 sec)
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.17.61.131 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql_bin.000038 Read_Master_Log_Pos: 4774 Relay_Log_File: slave_relay_bin.000002 Relay_Log_Pos: 2624 Relay_Master_Log_File: mysql_bin.000038 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: db1 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: 4774 Relay_Log_Space: 2831 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 10000 Master_UUID: 8d8746fb-2cc6-11e8-b1b6-000c295c63e0 Master_Info_File: /u01/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
Note that the Replicate_Do_DB: db1 value has come into play.
To cancel this setting:
slave:
mysql> stop slave sql_thread; Query OK, 0 rows affected (0.00 sec) mysql> change replication filter replicate_do_db=(); Query OK, 0 rows affected (0.00 sec) mysql> start slave sql_thread; Query OK, 0 rows affected (0.01 sec)
It should be noted that due to the setting of replication filtering, many transactions in the master library are not synchronized to the slave during the start of replication filtering, and the master and slave will definitely be out of synchronization. After canceling the replication filter, you need to manually resynchronize the data according to the actual situation.