
mariadb的主從複制叢集,預設情況下是把主庫上的所有庫進行複制,隻要在主庫上産生寫操作,從庫基于主庫的二進制日志做重放,進而實作把主庫的上的庫表複制到從庫;複制過濾器指的是我們僅複制一個或幾個資料庫相關的資料,而非所有;過濾器的作用就是來定義我們要複制那些庫,那些表,這種定義過濾器的方式叫白名單機制;除了這種告訴伺服器我們要複制的庫表的,當然我們也可以告訴伺服器我們不需要複制的庫或表,或者需要忽略的庫表的機制叫黑名單;在定義我們需要複制的庫或者表,我們可以在主庫上定義,也可以在從庫上定義;不同的是在主庫上定義,我們隻能定義需要複制的那些庫,忽略那些庫;
mariadb的主從複制叢集,預設情況下是把主庫上的所有庫進行複制,隻要在主庫上産生寫操作,從庫基于主庫的二進制日志做重放,進而實作把主庫的上的庫表複制到從庫;複制過濾器指的是我們僅複制一個或幾個資料庫相關的資料,而非所有;過濾器的作用就是來定義我們要複制那些庫,那些表,這種定義過濾器的方式叫白名單機制;除了這種告訴伺服器我們要複制的庫表的,當然我們也可以告訴伺服器我們不需要複制的庫或表,或者需要忽略的庫表的機制叫黑名單;在定義我們需要複制的庫或者表,我們可以在主庫上定義,也可以在從庫上定義;不同的是在主庫上定義,我們隻能定義需要複制的那些庫,忽略那些庫;其實在主庫上定義過濾器,就是告訴主庫那些庫的操作不記錄二進制日志,當然在主庫上不記錄二進制,也就無法實作複制;這種在主庫上定義那些庫記錄二進制,那些庫不記錄二進制,這種方式不是很推薦,原因在于如果主庫當機,做恢複操作時,我們不能利用二進制日志來把資料全部恢複;通常情況下我們在從庫上定義複制那些庫或者忽略那些庫,這裡需要注意一點,我們要麼使用白名單機制,要麼使用黑名單機制,不建議混合使用;
在主庫上配置隻允許某些庫記錄二進制日志,用binlog_do_db變量來指定,如下
提示:以上配置表示隻記錄和first_db庫相關寫操作的事件到二進制日志中;
重新開機主庫,然後在主庫上建立其他庫,看看是否還會同步到從庫呢?
提示:可以看到我們重新開機主庫後,在主庫上建立mydb庫,在從庫上并沒有看到mydb從主庫上複制過來;
在主庫上操作first_db庫,看看是否及時同步到從庫呢?
提示:可以看到在主庫上操作first_db中的表,在從庫上是能夠及時的看到;
配置在主庫上忽略某些庫,剩餘所有庫都記錄二進制日志
提示:以上配置表示忽略first_db庫上的二進制日志記錄,剩餘其他庫都要做二進制日志記錄;
測試:重新開機主庫,在主庫上操作first_db庫,看看是否能夠及時同步到從庫?
提示:可以看到我們在主庫上操作first_db庫中的student表,沒有被同步到從庫的;
檢視主庫是binlog否記錄了我們剛才的插入操作的語句
提示:在主庫上看二進制日志,并沒有記錄我們剛才的插如語句相關的操作;
在主庫上操作其他庫看看是否能夠被同步到從庫呢?
提示:可以看到我們在主庫上建立新的庫表是能夠及時同步到從庫;
在從庫上配置隻複制first_db相關寫操作事件
[root@docker-node03 ~]# 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指定;使用方式和上面白名單機制的使用方式一樣;如果需要永久生效,請把以上變量寫到配置檔案中重新開機服務即可生效;
作者:Linux-1874
出處:https://www.cnblogs.com/qiuhom-1874/
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利.