TiDB Data Migration Binlog Event Filter

TiDB Data Migration (DM) provides the binlog event filter feature to filter out or only receive specified types of binlog events for some schemas or tables. For example, you can filter out all TRUNCATE TABLE or INSERT events. The binlog event filter feature is more fine-grained than the block and allow lists feature.

Configure the binlog event filter

In the task configuration file, add the following configuration:

  1. filters:
  2. rule-1:
  3. schema-pattern: "test_*"
  4. table-pattern: "t_*"
  5. events: ["truncate table", "drop table"]
  6. sql-pattern: ["^DROP\\s+PROCEDURE", "^CREATE\\s+PROCEDURE"]
  7. action: Ignore

Starting from DM v2.0.2, you can configure the binlog event filter in the source configuration file. For details, see Upstream Database Configuration File.

When you use the wildcard for matching schemas and tables, note the following:

  • schema-pattern and table-pattern only support wildcards, including *, ?, and []. There can only be one * symbol in a wildcard match, and it must be at the end. For example, in table-pattern: "t_*", "t_*" indicates all tables starting with t_. See wildcard matching#Syntax) for details.

  • sql-pattern only supports regular expressions.

Parameter descriptions

  • schema-pattern/table-pattern: the binlog events or DDL SQL statements of upstream MySQL or MariaDB instance tables that match schema-pattern/table-pattern are filtered by the rules below.

  • events: the binlog event array. You can only select one or more Events from the following table:

    EventsTypeDescription
    allIncludes all the events below
    all dmlIncludes all DML events below
    all ddlIncludes all DDL events below
    noneIncludes none of the events below
    none ddlIncludes none of the DDL events below
    none dmlIncludes none of the DML events below
    insertDMLThe INSERT DML event
    updateDMLThe UPDATE DML event
    deleteDMLThe DELETE DML event
    create databaseDDLThe CREATE DATABASE DDL event
    drop databaseDDLThe DROP DATABASE DDL event
    create tableDDLThe CREATE TABLE DDL event
    create indexDDLThe CREATE INDEX DDL event
    drop tableDDLThe DROP TABLE DDL event
    truncate tableDDLThe TRUNCATE TABLE DDL event
    rename tableDDLThe RENAME TABLE DDL event
    drop indexDDLThe DROP INDEX DDL event
    alter tableDDLThe ALTER TABLE DDL event
  • sql-pattern: it is used to filter specified DDL SQL statements. The matching rule supports using a regular expression. For example, "^DROP\\s+PROCEDURE".

  • action: the string (Do/Ignore). Based on the following rules, it judges whether to filter. If either of the two rules is satisfied, the binlog is filtered; otherwise, the binlog is not filtered.

    • Do: the allow list. The binlog is filtered in either of the following two conditions:
      • The type of the event is not in the event list of the rule.
      • The SQL statement of the event cannot be matched by sql-pattern of the rule.
    • Ignore: the block list. The binlog is filtered in either of the following two conditions:
      • The type of the event is in the event list of the rule.
      • The SQL statement of the event can be matched by sql-pattern of the rule.
    • When multiple rules match the same table, the rules are applied sequentially. The block list has a higher priority than the allow list. For example, if both the Ignore and Do rules are applied to the same table, the Ignore rule takes effect.

Usage examples

This section shows the usage examples in the scenario of sharding (sharded schemas and tables).

Filter all sharding deletion operations

To filter out all deletion operations, configure the following two filtering rules:

  • filter-table-rule filters out the TRUNCATE TABLE, DROP TABLE and DELETE STATEMENT operations of all tables that match the test_*.t_* pattern.
  • filter-schema-rule filters out the DROP DATABASE operation of all schemas that match the test_* pattern.
  1. filters:
  2. filter-table-rule:
  3. schema-pattern: "test_*"
  4. table-pattern: "t_*"
  5. events: ["truncate table", "drop table", "delete"]
  6. action: Ignore
  7. filter-schema-rule:
  8. schema-pattern: "test_*"
  9. events: ["drop database"]
  10. action: Ignore

Only migrate sharding DML statements

To only migrate sharding DML statements, configure the following two filtering rules:

  • do-table-rule only migrates the CREATE TABLE, INSERT, UPDATE and DELETE statements of all tables that match the test_*.t_* pattern.
  • do-schema-rule only migrates the CREATE DATABASE statement of all schemas that match the test_* pattern.

Binlog Event Filter - 图1

Note

The reason why the CREATE DATABASE/TABLE statement is migrated is that you can migrate DML statements only after the schema and table are created.

  1. filters:
  2. do-table-rule:
  3. schema-pattern: "test_*"
  4. table-pattern: "t_*"
  5. events: ["create table", "all dml"]
  6. action: Do
  7. do-schema-rule:
  8. schema-pattern: "test_*"
  9. events: ["create database"]
  10. action: Do

Filter out the SQL statements that TiDB does not support

To filter out the PROCEDURE statements that TiDB does not support, configure the following filter-procedure-rule:

  1. filters:
  2. filter-procedure-rule:
  3. schema-pattern: "test_*"
  4. table-pattern: "t_*"
  5. sql-pattern: ["^DROP\\s+PROCEDURE", "^CREATE\\s+PROCEDURE"]
  6. action: Ignore

filter-procedure-rule filters out the ^CREATE\\s+PROCEDURE and ^DROP\\s+PROCEDURE statements of all tables that match the test_*.t_* pattern.

Filter out the SQL statements that the TiDB parser does not support

For the SQL statements that the TiDB parser does not support, DM cannot parse them and get the schema/table information. So you must use the global filtering rule: schema-pattern: "*".

Binlog Event Filter - 图2

Note

To avoid filtering out data that need to be migrated, you must configure the global filtering rule as strictly as possible.

To filter out the PARTITION statements that the TiDB parser (of some version) does not support, configure the following filtering rule:

  1. filters:
  2. filter-partition-rule:
  3. schema-pattern: "*"
  4. sql-pattern: ["ALTER\\s+TABLE[\\s\\S]*ADD\\s+PARTITION", "ALTER\\s+TABLE[\\s\\S]*DROP\\s+PARTITION"]
  5. action: Ignore