在線修改主從複製選項

前言: mysql

MySQL最經常使用的架構就是主從複製了,其實主從複製有不少選項,特別是在從庫端,咱們能夠設置複製過濾,好比說忽略某張表或某個庫。這些過濾選項都是能夠在線修改而不用重啓的。原來對這塊瞭解很少,最近看了下相關資料,我的以爲這個功能仍是很方便的,本篇文章會將這塊內容分享給你們。sql

1.複製過濾參數介紹

首先咱們要了解設置複製過濾的不一樣參數。複製過濾是在從庫端設置的,能夠只複製某些庫或某些表,也能夠忽略複製某些庫或某些表。這些都是由不一樣參數控制的,下面簡單介紹下不一樣參數的做用。segmentfault

  • 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語句在線修改複製過濾選項的方法,列舉的那些過濾參數均可以使用該語句修改,不過要注意有些選項是互斥的。應該按照實際需求去設置合適的參數,下面給出官方文檔中的示範語法:spa

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

總結: code

本篇文章介紹瞭如何在線更改複製過濾選項的方法,不一樣的過濾參數有不一樣的用途,若是你確實有需求要設置過濾參數,建議必定要進行全面測試,某些參數設置後可能影響到其餘庫表的複製。若是想永久生效,能夠在線修改後再加入配置文件內,這樣從庫重啓後仍是生效的。
公衆號.jpgorm

相關文章
相關標籤/搜索