mysql 5.7复制过滤CHANGE REPLICATION FILTER

mysql 5.7在配置复制过滤时比之前的版本有了些进步,不需要重新启动mysql,直接通过change replication filter语句就可以了。

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

在slave库中可以用这个命令来过滤master库的语句。

--REPLICATE_DO_DB和REPLICATE_IGNORE_DB分别指定需要同步的以及不需要同步的DB。

--REPLICATE_DO_TABLE和REPLICATE_IGNORE_TABLE分别指定需要同步的表以及不需要同步的表,5.7.5以后必须在表前指定DB name,如DB1.tb1_name。

-REPLICATE_WILD_DO_TABLE和REPLICATE_WILD_IGNORE_TABLE可以用来模糊匹配需要同步以及不需要同步的表名,在设定时必须用单引号。

--REPLICATE_REWRITE_DB必须设置一组DB,在savle库中用新的DB来替换master库中对应的DB操作。

除了用命令的方法指定过滤也可以在mysql启动时用参数的方法,比如 mysql -uroot -p --replicate_do_db=(db1),这个方法需要重启数据库。


下面测试一下这个功能,仅测试replicate_do_db的功能,其它的都大同小异。

1.默认情况下master所有的操作都会同步到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.设置 REPLICATE_DO_DB和 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
#必须先停止sql_thread进程
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)

slave库只有db1的数据同步过来了。

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)

注意Replicate_Do_DB: db1的值已经开始起作用了。

如果要取消这个设定:

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)

需要注意的是由于设置了复制过滤,在开始复制过滤期间master库的许多事务都没有同步到slave,主从肯定会不同步的。取消复制过滤后需要根据实际情况手工重新同步数据。

猜你喜欢

转载自blog.csdn.net/jolly10/article/details/80014181
今日推荐