1. 程式人生 > 實用技巧 >Mariadb之複製過濾器

Mariadb之複製過濾器

  mariadb的主從複製叢集,預設情況下是把主庫上的所有庫進行復制,只要在主庫上產生寫操作,從庫基於主庫的二進位制日誌做重放,從而實現把主庫的上的庫表複製到從庫;複製過濾器指的是我們僅複製一個或幾個資料庫相關的資料,而非所有;過濾器的作用就是來定義我們要複製那些庫,那些表,這種定義過濾器的方式叫白名單機制;除了這種告訴伺服器我們要複製的庫表的,當然我們也可以告訴伺服器我們不需要複製的庫或表,或者需要忽略的庫表的機制叫黑名單;在定義我們需要複製的庫或者表,我們可以在主庫上定義,也可以在從庫上定義;不同的是在主庫上定義,我們只能定義需要複製的那些庫,忽略那些庫;其實在主庫上定義過濾器,就是告訴主庫那些庫的操作不記錄二進位制日誌,當然在主庫上不記錄二進位制,也就無法實現複製;這種在主庫上定義那些庫記錄二進位制,那些庫不記錄二進位制,這種方式不是很推薦,原因在於如果主庫宕機,做恢復操作時,我們不能利用二進位制日誌來把資料全部恢復;通常情況下我們在從庫上定義複製那些庫或者忽略那些庫,這裡需要注意一點,我們要麼使用白名單機制,要麼使用黑名單機制,不建議混合使用;

  在主庫上配置只允許某些庫記錄二進位制日誌,用binlog_do_db變數來指定,如下

  提示:以上配置表示只記錄和first_db庫相關寫操作的事件到二進位制日誌中;

  重啟主庫,然後在主庫上建立其他庫,看看是否還會同步到從庫呢?

  提示:可以看到我們重啟主庫後,在主庫上新建mydb庫,在從庫上並沒有看到mydb從主庫上覆制過來;

  在主庫上操作first_db庫,看看是否及時同步到從庫呢?

  提示:可以看到在主庫上操作first_db中的表,在從庫上是能夠及時的看到;

  配置在主庫上忽略某些庫,剩餘所有庫都記錄二進位制日誌

  提示:以上配置表示忽略first_db庫上的二進位制日誌記錄,剩餘其他庫都要做二進位制日誌記錄;

  測試:重啟主庫,在主庫上操作first_db庫,看看是否能夠及時同步到從庫?

  提示:可以看到我們在主庫上操作first_db庫中的student表,沒有被同步到從庫的;

  檢視主庫是binlog否記錄了我們剛才的插入操作的語句

  提示:在主庫上看二進位制日誌,並沒有記錄我們剛才的插如語句相關的操作;

  在主庫上操作其他庫看看是否能夠被同步到從庫呢?

  提示:可以看到我們在主庫上建立新的庫表是能夠及時同步到從庫;

  在從庫上配置只複製first_db相關寫操作事件

[[email protected] ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 22
Server version: 5.5.65-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show global variables like 'replicate%';
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| replicate_annotate_row_events | OFF |
| replicate_do_db | |
| replicate_do_table | |
| replicate_events_marked_for_skip | replicate |
| replicate_ignore_db | |
| replicate_ignore_table | |
| replicate_wild_do_table | |
| replicate_wild_ignore_table | |
+----------------------------------+-----------+
8 rows in set (0.00 sec) MariaDB [(none)]> set @@global.replicate_do_db=first_db;
ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> set @@global.replicate_do_db=first_db;
Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> show global variables like 'replicate%';
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| replicate_annotate_row_events | OFF |
| replicate_do_db | first_db |
| replicate_do_table | |
| replicate_events_marked_for_skip | replicate |
| replicate_ignore_db | |
| replicate_ignore_table | |
| replicate_wild_do_table | |
| replicate_wild_ignore_table | |
+----------------------------------+-----------+
8 rows in set (0.00 sec) MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.22
Master_User: rpluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000017
Read_Master_Log_Pos: 245
Relay_Log_File: relay-log.000035
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000017
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: first_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: 245
Relay_Log_Space: 1101
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: 1
1 row in set (0.00 sec) MariaDB [(none)]>

  提示:以上是在從節點上直接線上修改replicate_do_db變數來實現複製過濾功能;如果需要永久生效,那麼把該配置寫到配置檔案中即可;以上配置表示只複製first_db相關寫操作的事件;線上修改變數的方式,需要先停止slave,修改完成後在啟動;從show slave status中,我們就能夠清楚的replicate_do_db的值為first_db;

  測試:在主庫上對其他庫操作,看看是否能夠同步到從節點?

  提示:可以看到我們在主庫刪除mydb2中的test表後,並沒有同步到從庫;

  測試:操作first_db中的表,看看是否能同步?

  提示:可以看到我們在first_db庫中的student表中插入了一條新資料,在從庫是能夠及時的同步;

  配置從節點只複製first_db庫中的student表的操作相關事件

MariaDB [first_db]> stop slave;
Query OK, 0 rows affected (0.01 sec) MariaDB [first_db]> show global variables like 'replicate%';
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| replicate_annotate_row_events | OFF |
| replicate_do_db | first_db |
| replicate_do_table | |
| replicate_events_marked_for_skip | replicate |
| replicate_ignore_db | |
| replicate_ignore_table | |
| replicate_wild_do_table | |
| replicate_wild_ignore_table | |
+----------------------------------+-----------+
8 rows in set (0.00 sec) MariaDB [first_db]> set @@global.replicate_do_table=first_db.student;
ERROR 1232 (42000): Incorrect argument type to variable 'replicate_do_table'
MariaDB [first_db]> set @@global.replicate_do_table='first_db.student';
Query OK, 0 rows affected (0.00 sec) MariaDB [first_db]> show global variables like 'replicate%';
+----------------------------------+------------------+
| Variable_name | Value |
+----------------------------------+------------------+
| replicate_annotate_row_events | OFF |
| replicate_do_db | first_db |
| replicate_do_table | first_db.student |
| replicate_events_marked_for_skip | replicate |
| replicate_ignore_db | |
| replicate_ignore_table | |
| replicate_wild_do_table | |
| replicate_wild_ignore_table | |
+----------------------------------+------------------+
8 rows in set (0.00 sec) MariaDB [first_db]> start slave;
Query OK, 0 rows affected (0.00 sec) MariaDB [first_db]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.22
Master_User: rpluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000017
Read_Master_Log_Pos: 1198
Relay_Log_File: relay-log.000050
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000017
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: first_db
Replicate_Ignore_DB:
Replicate_Do_Table: first_db.student
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1198
Relay_Log_Space: 1101
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: 1
1 row in set (0.00 sec) MariaDB [first_db]>

  提示:replicate_do_table這個變數線上修改時需要指明那個庫中的那個表,並用引號引起來,否則會提示我們引數型別不正確;

  測試:在主庫的first_db庫中新建一張表,看看是否能夠被同步到從庫?

  提示:可以看到我們在主庫的fisrt_db中新建一張test表,並沒有同步到從庫中去,這說明replicate_do_table的優先順序要高於replicate_do_db;

  在主庫的first_db庫中對student表做插入操作,看看是否能夠被同步到從庫?

  提示:可以看到操作first_db中的student是能夠及時同步到從庫;

  設定從節點只複製以student開頭的表

MariaDB [first_db]> stop slave;
Query OK, 0 rows affected (0.00 sec) MariaDB [first_db]> show global variables like 'replicate%';
+----------------------------------+------------------+
| Variable_name | Value |
+----------------------------------+------------------+
| replicate_annotate_row_events | OFF |
| replicate_do_db | first_db |
| replicate_do_table | first_db.student |
| replicate_events_marked_for_skip | replicate |
| replicate_ignore_db | |
| replicate_ignore_table | |
| replicate_wild_do_table | |
| replicate_wild_ignore_table | |
+----------------------------------+------------------+
8 rows in set (0.00 sec) MariaDB [first_db]> set @@global.replicate_wild_do_table='first_db.student%';
Query OK, 0 rows affected (0.00 sec) MariaDB [first_db]> show global variables like 'replicate%';
+----------------------------------+-------------------+
| Variable_name | Value |
+----------------------------------+-------------------+
| replicate_annotate_row_events | OFF |
| replicate_do_db | first_db |
| replicate_do_table | first_db.student |
| replicate_events_marked_for_skip | replicate |
| replicate_ignore_db | |
| replicate_ignore_table | |
| replicate_wild_do_table | first_db.student% |
| replicate_wild_ignore_table | |
+----------------------------------+-------------------+
8 rows in set (0.01 sec) MariaDB [first_db]> start slave;
Query OK, 0 rows affected (0.00 sec) MariaDB [first_db]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.22
Master_User: rpluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000017
Read_Master_Log_Pos: 1198
Relay_Log_File: relay-log.000051
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000017
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: first_db
Replicate_Ignore_DB:
Replicate_Do_Table: first_db.student
Replicate_Ignore_Table:
Replicate_Wild_Do_Table: first_db.student%
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1198
Relay_Log_Space: 1101
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: 1
1 row in set (0.00 sec) MariaDB [first_db]>

  測試:在主庫first_db庫中操作非student開頭的表,看看是否能夠被複制到從庫呢?

  提示:可以看到在主庫的first_db庫中新增test2表,並沒有被同步到從庫;

  測試:在主庫新增student_test表,看看是否被同步到從庫?

  提示:可以看到在主庫上新建student_test表被同步到從庫中去了;

  以上就是mariadb/mysql資料庫主從複製中的複製過濾器的使用;以上演示部分使用白名單機制進行演示的,黑名單和白名單配置方式相同,只不過黑名單表示忽略指定的庫或者表,其他剩下的庫和表都要進行復制,而白名單是指定的庫和表都要複製,其他剩餘的庫或表都不被複制;在從節點使用黑名單機制忽略庫使用replicate_ignore_db變數指定;忽略某些表使用replicate_ignore_table變數來指定;忽略以某類表使用replicate_ignore_wild_do_table指定;使用方式和上面白名單機制的使用方式一樣;如果需要永久生效,請把以上變數寫到配置檔案中重啟服務即可生效;