暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

什么是MySQL中的复制过滤器?

原创 Shubing Wu 2022-10-20
911

Replication Filters用于筛选将在副本中复制的必要数据库和表,可以在my.cnf或命令行进行设置。在数据库中所做的更改将记录在二进制日志文件中,过滤器可以在源(使用- binlog-*选项)和副本(使用- replication -*选项)中创建。


MySQL  Replication Filters 的类型 ?

1、二进制日志过滤器

  • Binlog_do_db
  • Binlog_ignore_db

2、复制过滤器

  • Replicate_do_db
  • Replicate_ignore_db
  • Replicate_do_table
  • Replicate_ignore_table
  • Replicate_wild_ignore_table
  • Replicate_rewrite_db

3、多源复制过滤器

Binary log Filter

在源服务器中,Binary log Filter用于过滤掉binlog文件中的写操作。通过Binary log Filter,我们可以控制binlog文件中的写操作,这也将同时复制到从服务器中。

1)Binlog_do_db

通过使用Binlog_do_db变量,二进制日志将只记录上述数据库所做的更改。若要指定多个数据库,请多次使用此选项,每个数据库一次,在my.cnf文件添加以下行

binlog_do_db=imdb

mysql> show master status;
±--------------±---------±-------------±-----------------±------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
±--------------±---------±-------------±-----------------±------------------+
| binlog.000003 | 456 | imdb | | |
±--------------±---------±-------------±-----------------±------------------+
1 row in set (0.00 sec)

2)Binlog_ignore_db

通过使用Binlog_ignore_db变量,二进制日志将忽略上述数据库所做的更改。这些数据库所做的更改不会记录在binlog文件中,在MySQL配置文件中,添加

binlog_ignore_db=imdb

mysql> show master status;
±--------------±---------±-------------±-----------------±------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
±--------------±---------±-------------±-----------------±------------------+
| binlog.000006 | 156 | | imdb | |
±--------------±---------±-------------±-----------------±------------------+
1 row in set (0.00 sec)

Replicate Filter

Replicate Filter将应用于复制服务器,您可以使用Replicate Filter限制副本中的复制过程。

1)Replicate_do_db

如果您想复制特定数据库所做的更改,那么可以在MySQL配置文件中添加Replicate_do_db变量。

replicate_do_db=imdb

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.168.211
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000005
Read_Master_Log_Pos: 156
Relay_Log_File: relay-log.000011
Relay_Log_Pos: 365
Relay_Master_Log_File: binlog.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: imdb
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:

2)Replicate_ignore_db

在副本服务器中,如果你想忽略特定数据库所做的更改,那么我们可以使用Replicate_ignore_db变量。这个变量将复制所有其他数据库所做的更改,除了特定的数据库。在MySQL配置文件中,添加

replicate_ignore_db=imdb

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.168.211
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000006
Read_Master_Log_Pos: 156
Relay_Log_File: relay-log.000015
Relay_Log_Pos: 321
Relay_Master_Log_File: binlog.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: imdb
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:

3)Replicate_do_table

在复制中,如果希望复制数据库中特定表所做的更改,则可以使用Replicate_do_table变量。在MySQL配置文件中,添加

replicate_do_table= imdb.movies

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.168.211
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000007
Read_Master_Log_Pos: 659
Relay_Log_File: relay-log.000018
Relay_Log_Pos: 868
Relay_Master_Log_File: binlog.000007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table: imdb.movies
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:

4)Replicate_ignore_table

在副本服务器中,如果您想忽略数据库中特定表所做的更改,那么可以使用Replicate_ignore_table。此变量将复制数据库中所有其他表的更改,上述表除外。在MySQL配置文件中,添加

replicate_Ignore_Table= imdb.actors

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.168.211
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000007
Read_Master_Log_Pos: 1517
Relay_Log_File: relay-log.000020
Relay_Log_Pos: 893
Relay_Master_Log_File: binlog.000007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table: imdb.actors
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:

5)Replicate_wild_do_table

复制线程将被限制复制匹配指定通配符模式的表。模式可以包含%和_通配符,它们与LIKE模式匹配操作符具有相同的含义。在MySQL配置文件中,添加以下内容

replicate_wild_do_table= imdb.movies%

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.168.211
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000007
Read_Master_Log_Pos: 2089
Relay_Log_File: relay-log.000022
Relay_Log_Pos: 893
Relay_Master_Log_File: binlog.000007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table: imdb.movies%
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 2089
Relay_Log_Space: 1096

6)Replicate_wild_ignore_table

复制线程将被禁止复制匹配指定通配符模式的表。Replica不会复制那些匹配上述通配符模式的表所做的更改。

在MySQL配置文件中,添加

replicate_wild_ignore_table= imdb.movies%

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.168.211
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000007
Read_Master_Log_Pos: 3730
Relay_Log_File: relay-log.000024
Relay_Log_Pos: 1181
Relay_Master_Log_File: binlog.000007
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: imdb.movies%
Last_Errno: 0
Last_Error:

7)Replicate_rewrite_db

如果源上的数据库是from_name,则创建复制筛选器的副本将默认数据库转换为to_name。如果from_name是源上的默认数据库,则只有涉及表的语句会受到影响。

在MySQL配置文件中,添加

replicate_rewrite_db=imdb->imdb_full

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.168.211
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000007
Read_Master_Log_Pos: 4228
Relay_Log_File: relay-log.000026
Relay_Log_Pos: 321
Relay_Master_Log_File: binlog.000007
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: 4228
Relay_Log_Space: 524
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
Master_UUID: 290a305f-a62f-11eb-8830-080027b81a94
Master_Info_File: mysql.slave_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: 290a305f-a62f-11eb-8830-080027b81a94:1-7
Auto_Position: 0
Replicate_Rewrite_DB: (imdb, imdb_full)

Multi-source replication Filter

在多源复制中,一个副本将有多个源。将为每个源创建每个通道。我们可以通过应用基于通道的复制过滤器在副本中筛选复制过程。如果相同的数据库或表出现在多个源上,并且副本希望从一个源复制它,那么基于通道的复制过滤器将非常有帮助。

假设我们有两个源-源1,源2,和一个副本,下面是为特定通道设置基于通道的复制筛选器的示例。这里我们对信道源2应用了滤波器。这里选择的数据库是imdb,选择的表是movies。

在MySQL配置文件中,添加

replicate_do_table=’source 2’:imdb.movies

mysql> stop slave for channel ‘source 2’;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CHANGE REPLICATION FILTER REPLICATE_DO_TABLE=(imdb.movies) FOR CHANNEL ‘source 2’;
Query OK, 0 rows affected (0.00 sec)

mysql> start slave for channel ‘source 2’;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show slave status for channel ‘source 2’\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.168.198
Master_User: repl_user2
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000002
Read_Master_Log_Pos: 4073
Relay_Log_File: relay-log-master@00202.000009
Relay_Log_Pos: 321
Relay_Master_Log_File: binlog.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table: imdb.movies
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 4073
Relay_Log_Space: 702
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: 3
Master_UUID: 1c94c9b1-c117-11eb-937d-080027c46d28
Master_Info_File: mysql.slave_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: 290a305f-a62f-11eb-8830-080027b81a94:1-7
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name: source 2
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 1
Network_Namespace:
1 row in set, 1 warning (0.00 sec)

希望,这个博客能帮助你详细了解MySQL复制过滤器的细节。要进一步参考过滤器使用参考手册 here.

原文标题:Replication Filters in MySQL an Overview
原文作者:Asuwini P
原文地址:https://mydbops.wordpress.com/2021/09/24/replication-filters-in-mysql-an-overview/

最后修改时间:2022-10-20 11:59:41
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论