Skip to content

MySQL binlog filtering — selective replication done right

How binlog filters work in MySQL — primary-side ignore options, replica-side do/ignore, schema vs table granularity, and the rules an MSP follows to avoid silent data divergence.

Replication filters let you exclude or include specific databases and tables from the binlog stream — useful when one replica is a partial copy (a reporting cluster, a regional shard, a sandbox). They are also one of the most common ways to produce a silently divergent replica. This article is the operator-grade view: where filters apply, the difference between binlog_* and replicate_*, the wildcard variants, and the rules we follow so a filter never makes the replica disagree with the primary in ways nobody notices until restoration day.

How to verify

Filters live on the primary (binlog-side) or the replica (apply-side). They appear in different system variable groups.

mysql -e "SHOW MASTER STATUS\G"
mysql -e "SHOW REPLICA STATUS\G" | grep -E '(Replicate_Do|Replicate_Ignore|Replicate_Wild)'
mysql -e "SHOW VARIABLES LIKE 'binlog_do_db';"
mysql -e "SHOW VARIABLES LIKE 'binlog_ignore_db';"
mysql -e "SELECT * FROM performance_schema.replication_applier_filters;"

SHOW REPLICA STATUS shows the active replica-side filters. Replicate_Do_DB, Replicate_Ignore_DB, Replicate_Do_Table, Replicate_Ignore_Table, Replicate_Wild_Do_Table, Replicate_Wild_Ignore_Table are the seven possible filter sets.

What’s happening

Filters apply at two stages. Primary-side filters (binlog_do_db, binlog_ignore_db) decide which changes the primary writes to its binlog. Once filtered out, the change does not exist in the binlog stream and no replica can ever receive it. The trade-off: these filters are coarse (database-level only) and surprising — they apply based on the current database (the result of the last USE), not on the schema-qualified table name. USE foo; INSERT INTO bar.t VALUES (...) follows the filter rule for foo, not for bar. Production should rarely use primary-side filters because of this.

Replica-side filters (replicate_do_db, replicate_ignore_db, replicate_do_table, replicate_ignore_table, replicate_wild_do_table, replicate_wild_ignore_table) decide which events the replica’s SQL thread applies. The events still arrive over the network and are written to the relay log — only the apply is skipped. These filters are richer: table-level filters, wildcard variants, and a precedence order. They are also where modern MySQL puts the operational documentation.

The wildcard variants are usually what you want. replicate_wild_do_table = app.user_% includes every table in app whose name starts with user_; the non-wildcard replicate_do_table = app.user_a requires the schema-qualified literal table name and breaks with the same USE quirk as the primary filters.

MySQL 8.0 added the ability to set filters at runtime per-channel:

CHANGE REPLICATION FILTER REPLICATE_WILD_DO_TABLE = ('app.user_%');

This is the production way — no restart needed and the filter is associated with the replication channel, not with the whole server.

The procedure

  1. Decide whether you need primary-side filtering or replica-side. The default answer in our engagements is replica-side. Primary-side filtering should be reserved for cases where the binlog volume cost itself is a problem (very large changes to data you do not want to replicate at all).

  2. Apply replica-side filters at runtime. Stop the replication SQL thread, set the filter, start it.

    STOP REPLICA SQL_THREAD;
    CHANGE REPLICATION FILTER
      REPLICATE_WILD_DO_TABLE = ('app.%', 'shared.%'),
      REPLICATE_WILD_IGNORE_TABLE = ('app.tmp_%', 'app.cache_%');
    START REPLICA SQL_THREAD;
  3. Persist the filters across restarts by writing them into my.cnf. The runtime values apply now; the config file ensures they survive.

    [mysqld]
    replicate-wild-do-table     = app.%
    replicate-wild-do-table     = shared.%
    replicate-wild-ignore-table = app.tmp_%
    replicate-wild-ignore-table = app.cache_%

    Each filter directive appears as its own line — replicate-wild-do-table = a.%,b.% does not work; the parser expects one pattern per line.

  4. Verify the filters are doing what you expect by inducing a change and watching the replica.

    -- on the primary
    INSERT INTO app.user_data VALUES (1, 'should replicate');
    INSERT INTO app.tmp_scratch VALUES (1, 'should NOT replicate');
    -- on the replica, after a beat
    SELECT * FROM app.user_data;
    SELECT * FROM app.tmp_scratch; -- should be empty
  5. Document the filter as part of the topology runbook. A replica with filters is not a full copy and the operator who comes next needs to know.

  6. For cross-region or sandbox setups where the filtered replica must ignore entire schemas, use the database-level variant — but state explicitly in the runbook that this replica cannot become primary.

    STOP REPLICA SQL_THREAD;
    CHANGE REPLICATION FILTER
      REPLICATE_IGNORE_DB = (audit_log, archive_2024);
    START REPLICA SQL_THREAD;

Common pitfalls

  • Primary-side binlog_do_db with cross-database statements — USE foo; UPDATE bar.t SET ... follows the filter rule for the current database (foo). The update can be filtered out or in depending on which database was last USEd, not based on the table being modified.
  • Filtered replica promoted to primary — silently divergent data. The replica that filtered out app.cache_* becomes the new primary; data that other replicas had now is missing from the new primary because nobody ever told them to clear it. Document and enforce “this replica is not a failover candidate.”
  • Forgetting to persist the filter in my.cnf — CHANGE REPLICATION FILTER survives STOP REPLICA/START REPLICA but not a mysqld restart unless written into the config. Many incidents start with “we restarted MySQL after a kernel upgrade and now the replica is replicating everything.”
  • Mixing replicate_do_* and replicate_ignore_* — the precedence order is documented but subtle. replicate_do_* is checked first; if it matches, the event applies; if not, replicate_ignore_* is checked. Operators who configure both often discover their filter does the opposite of what they intended.
  • No alerting on filter drift — if someone changes the filter on one replica but not the others, half your replicas have data the others lack. Monitoring should compare the filter set across the topology.

Replication filters are useful when you genuinely need a partial copy — a reporting cluster, a regional shard, a sandbox — and dangerous when used to “trim” replication for performance without thinking through the failover implications. In the engagements we run, filtered replicas are clearly marked in the topology, the replicate_* settings are checked in as code, and the failover plan documents which replicas are full copies and which are not. For the broader pattern of operating MySQL replication topologies, see /en/solutions/clustered-environments/.