mysql如何在线修改主从复制选项

前言:

MySQL最常用的架构就是主从复制了,其实主从复制有很多选项,特别是在从库端,我们可以设置复制过滤,比如说忽略某张表或某个库。这些过滤选项都是可以在线修改而不用重启的。原来对这块了解不多,最近看了下相关资料,个人觉得这个功能还是很方便的,本篇文章会将这块内容分享给大家。

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

mysql如何在线修改主从复制选项

扫一扫手机访问