Filter Binlog Events
This document describes how to filter binlog events when you use DM to perform continuous incremental data replication. For the detailed replication instructions, refer to the following documents by scenarios:
- Migrate Small Datasets from MySQL to TiDB
- Migrate Large Datasets from MySQL to TiDB
- Migrate and Merge MySQL Shards of Small Datasets to TiDB
- Migrate and Merge MySQL Shards of Large Datasets to TiDB
Configuration
To use binlog event filters, add the filters field to the DM task configuration file, as shown below:
filters:
rule-1:
schema-pattern: "test_*"
table-pattern: "t_*"
events: ["truncate table", "drop table"]
action: Ignore
schema-pattern/table-pattern: Filters matching schemas or tablesevents: Filters binlog events. Supported events are listed in the table below:sql-pattern: Filters specified DDL SQL statements. The matching rule supports using a regular expression.action:DoorIgnoreDo: the allow list. A binlog event is replicated if meeting either of the following two conditions:- The event matches the rule setting.
- sql-pattern has been specified and the SQL statement of the event matches any of the sql-pattern options.
Ignore: the block list. A binlog event is filtered out if meeting either of the following two conditions:- The event matches the rule setting. - sql-pattern has been specified and the SQL statement of the event matches any of the sql-pattern options.If both
DoandIgnoreare configured,Ignorehas higher priority overDo. That is, an event satisfying bothIgnoreandDoconditions will be filtered out.
Application scenarios
This section describes the application scenarios of binlog event filter.
Filter out all sharding deletion operations
To filter out all deletion operations, configure a filter-table-rule and a filter-schema-rule, as shown below:
filters:
filter-table-rule:
schema-pattern: "test_*"
table-pattern: "t_*"
events: ["truncate table", "drop table", "delete"]
action: Ignore
filter-schema-rule:
schema-pattern: "test_*"
events: ["drop database"]
action: Ignore
Migrate only DML operations of sharded schemas and tables
To replicate only DML statements, configure two Binlog event filter rule, as shown below:
filters:
do-table-rule:
schema-pattern: "test_*"
table-pattern: "t_*"
events: ["create table", "all dml"]
action: Do
do-schema-rule:
schema-pattern: "test_*"
events: ["create database"]
action: Do
Handle SQL statements that TiDB does not support or that DM cannot parse
You do not need to configure filters for CREATE PROCEDURE, DROP PROCEDURE, or ALTER PROCEDURE. DM skips these statements automatically.
When you add or drop a partition in MySQL, DM normally replicates the change to TiDB. You do not need a filter for partition operations that both DM and TiDB support. To check which operations TiDB supports for your table's partition type, see Partition management.
If DM reports a parsing error for an ALTER TABLE statement that uses ADD PARTITION or DROP PARTITION and you do not need to replicate the partition change to TiDB, you can use the following rule to skip the statement. A parsing error in DM does not necessarily mean that TiDB does not support the operation.
When DM cannot parse a statement, it cannot apply filters that target a specific schema or table. In this case, you need a global filtering rule with schema-pattern: "*" and no table-pattern. This rule applies to matching statements for all tables in the migration task.
filters:
filter-partition-rule:
schema-pattern: "*"
sql-pattern: ["ALTER\\s+TABLE[\\s\\S]*ADD\\s+PARTITION", "ALTER\\s+TABLE[\\s\\S]*DROP\\s+PARTITION"]
action: Ignore