Managing MySQL replication filters in a live production environment doesn’t have to mean rebuilding your replicas from scratch. In this post, we’ll walk through how to safely exclude specific tables from replication and cleanly re-sync and add live tables back into the replication stream using mydumper and binlog coordinates, without ever touching the primary server.
In MySQL, the primary-replica (formerly master-slave) architecture is the most widely used deployment pattern. Replication filters let you build replicas that hold only the data a team needs. Some teams need transaction data, others need analytics data, and a multi-source replica may combine data from several sources. Replicating less also reduces resource use.
Needs change over time, so you will often have to update a replica’s filters, either to stop replicating some tables or schemas, or to start replicating them again.
Stopping replication for the tables/schemas is easy. Just stop the replication, add/update the tables/schemas in the replication filters, and start the replication. See the example below to stop replication for the table sbtest.sbtest3.
Example:
Replication status before adding the filters:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
*************************** 1. row *************************** Replica_IO_State: Waiting for source to send event Source_Host: 172.31.92.195 Source_User: repl_user Source_Port: 3306 Source_Log_File: binlog.000023 Read_Source_Log_Pos: 802000814 Relay_Log_File: ip-172-31-88-61-relay-bin.000012 Relay_Log_Pos: 285467877 Relay_Source_Log_File: binlog.000022 Replica_IO_Running: Yes Replica_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Error: Exec_Source_Log_Pos: 285467666 Relay_Log_Space: 1875763218 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Seconds_Behind_Source: 868 Last_IO_Error: Last_SQL_Error: |
Adding the table sbtest.sbtest3 to the replication filters:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
mysql> STOP REPLICA; Query OK, 0 rows affected (0.02 sec) mysql> CHANGE REPLICATION FILTER REPLICATE_IGNORE_TABLE = (sbtest.sbtest3); Query OK, 0 rows affected (0.00 sec) mysql> START REPLICA; Query OK, 0 rows affected (0.01 sec) mysql> show replica status\G *************************** 1. row *************************** Replica_IO_State: Waiting for source to send event Source_Host: 172.31.92.195 Source_User: repl_user Source_Port: 3306 Source_Log_File: binlog.000023 Read_Source_Log_Pos: 945759470 Relay_Log_File: ip-172-31-88-61-relay-bin.000012 Relay_Log_Pos: 342686043 Relay_Source_Log_File: binlog.000022 Replica_IO_Running: Yes Replica_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: sbtest.sbtest3 Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Error: Exec_Source_Log_Pos: 342685832 Relay_Log_Space: 2019522262 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Seconds_Behind_Source: 914 Last_IO_Error: Last_SQL_Error: |
Hashing the same value three times means the table is no longer receiving writes from the primary.
|
1 2 3 4 5 6 7 8 9 10 11 |
mysql> pager md5sum; PAGER set to 'md5sum' mysql> select * from sbtest.sbtest3; b3454182d67ebeb29a453cc18f5d50e6 - 100000 rows in set (0.38 sec) mysql> select * from sbtest.sbtest3; b3454182d67ebeb29a453cc18f5d50e6 - 100000 rows in set (0.07 sec) mysql> select * from sbtest.sbtest3; b3454182d67ebeb29a453cc18f5d50e6 - 100000 rows in set (0.07 sec) |
When the requirement is to add or update a table or database in the filters to replicate data from the primary to the replica, we need to work carefully; a small mistake can cause a replication error on the replica, and we may end up rebuilding it from scratch.
For this, we usually think that just taking a dump backup from the primary or another replica and restoring it on the replica may work. But this will not be enough for the live tables/databases. We also need to stop the replication and start replication with the correct file/position. The high-level plan for this task is below.
Here is an example of how I added the table sbtest.sbtest3 back into the replication.
The target replica uses multi-threaded replication. START REPLICA UNTIL SQL_AFTER_MTS_GAPS lets the parallel applier threads fill any gaps, ensuring the replica’s position is consistent, after which the SQL thread stops automatically. With a single applier thread, you can skip this and run STOP REPLICA.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
mysql> STOP REPLICA; Query OK, 0 rows affected (0.01 sec) mysql> START REPLICA UNTIL SQL_AFTER_MTS_GAPS; Query OK, 0 rows affected (0.01 sec) mysql> show replica status\G *************************** 1. row *************************** Replica_IO_State: Waiting for source to send event Source_Host: 172.31.92.195 Source_User: repl_user Source_Port: 3306 Source_Log_File: binlog.000026 Read_Source_Log_Pos: 660328628 Relay_Log_File: ip-172-31-88-61-relay-bin.000017 Relay_Log_Pos: 212025835 Relay_Source_Log_File: binlog.000023 Replica_IO_Running: Yes Replica_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: sbtest.sbtest3 Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Error: Exec_Source_Log_Pos: 904847198 Relay_Log_Space: 3881619155 Until_Condition: DONE Until_Log_File: Until_Log_Pos: 0 Seconds_Behind_Source: NULL |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
mysql> stop replica; Query OK, 0 rows affected (0.01 sec) mysql> show replica status\G *************************** 1. row *************************** Replica_IO_State: Source_Host: 172.31.92.195 Source_User: repl_user Source_Port: 3306 Source_Log_File: binlog.000026 Read_Source_Log_Pos: 665926304 Relay_Log_File: ip-172-31-88-61-relay-bin.000017 Relay_Log_Pos: 212025835 Relay_Source_Log_File: binlog.000023 Replica_IO_Running: No Replica_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: sbtest.sbtest3 Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Error: Exec_Source_Log_Pos: 904847198 Relay_Log_Space: 3887216831 Until_Condition: DONE Until_Log_File: Until_Log_Pos: 0 Seconds_Behind_Source: NULL Last_IO_Error: Last_SQL_Error: |
Take a dump from another replica using mydumper with the –replica-data option to capture the primary’s binlog file and position. This allows you to avoid touching the primary.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
root@ip-172-31-88-61:/home/ubuntu# mydumper --host=172.31.94.154 -u root -p Percona@1234 --tables-list=sbtest.sbtest3 --trx-tables --threads=4 --outputdir=/tmp/mydump_export --replica-data ** (mydumper:13763): WARNING **: 07:03:16.178: Using --trx-tables options, binlog coordinates will not be accurate if you are writing to non transactional tables. root@ip-172-31-88-61:/home/ubuntu# echo $? 0 root@ip-172-31-88-61:/home/ubuntu# ls -lrth /tmp/mydump_export total 19M -rw-r----- 1 root root 405 Oct 31 07:03 sbtest-schema-create.sql -rw-r----- 1 root root 548 Oct 31 07:03 sbtest.sbtest3-schema.sql -rw-r----- 1 root root 4.8M Oct 31 07:03 sbtest.sbtest3.00002.sql -rw-r----- 1 root root 4.8M Oct 31 07:03 sbtest.sbtest3.00000.sql -rw-r----- 1 root root 5.4M Oct 31 07:03 sbtest.sbtest3.00001.sql -rw-r----- 1 root root 4.2M Oct 31 07:03 sbtest.sbtest3.00003.sql -rw-r--r-- 1 root root 2.6K Oct 31 07:03 metadata root@ip-172-31-88-61:/home/ubuntu# |
The metadata file records where the primary’s binary log was when the dump was taken. We need that position for the next step, to stop the target replica at the same point.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
root@ip-172-31-88-61:/home/ubuntu# cat /tmp/mydump_export/metadata # Started dump at: 2026-10-31 07:03:16 [config] quote-character = BACKTICK [myloader_session_variables] SQL_MODE='NO_AUTO_VALUE_ON_ZERO,ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION' /*!40101 [source] # Channel_Name = '' # It can be use to setup replication FOR CHANNEL # SOURCE_LOG_FILE = "ip-172-31-94-154-bin.000006" # SOURCE_LOG_POS = 655712715 [replication] SOURCE_LOG_FILE = "binlog.000024" SOURCE_LOG_POS = 950744300 #SOURCE_AUTO_POSITION = {0|1} # Replica_IO_State = 'Waiting for source to send event' # Source_Host = '172.31.92.195' # Source_User = 'repl_user' # Source_Port = 3306 # Connect_Retry = 60 |
Get the primary binlog and position from the metadata file, start replication until the primary binlog and position match, and wait a while for it to sync with the file and position.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
mysql> show replica status\G *************************** 1. row *************************** Replica_IO_State: Source_Host: 172.31.92.195 Source_User: repl_user Source_Port: 3306 Source_Log_File: binlog.000026 Read_Source_Log_Pos: 665926304 Relay_Log_File: ip-172-31-88-61-relay-bin.000017 Relay_Log_Pos: 212025835 Relay_Source_Log_File: binlog.000023 Replica_IO_Running: No Replica_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: sbtest.sbtest3 Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Error: Exec_Source_Log_Pos: 904847198 Relay_Log_Space: 3887216831 Until_Condition: DONE Until_Log_File: Until_Log_Pos: 0 Seconds_Behind_Source: NULL Last_IO_Error: Last_SQL_Error: mysql> START REPLICA UNTIL SOURCE_LOG_FILE='binlog.000024', SOURCE_LOG_POS=950744300; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> show replica status\G *************************** 1. row *************************** Replica_IO_State: Waiting for source to send event Source_Host: 172.31.92.195 Source_User: repl_user Source_Port: 3306 Source_Log_File: binlog.000026 Read_Source_Log_Pos: 722233290 Relay_Log_File: ip-172-31-88-61-relay-bin.000017 Relay_Log_Pos: 213572791 Relay_Source_Log_File: binlog.000023 Replica_IO_Running: Yes Replica_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: sbtest.sbtest3 Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Error: Exec_Source_Log_Pos: 906394154 Relay_Log_Space: 3943524205 Until_Condition: Source Until_Log_File: binlog.000024 Until_Log_Pos: 950744300 Seconds_Behind_Source: 2986 Last_IO_Error: Last_SQL_Error: |
After some time, the SQL thread stopped once it reached the file and position
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
mysql> show replica status\G *************************** 1. row *************************** Replica_IO_State: Waiting for source to send event Source_Host: 172.31.92.195 Source_User: repl_user Source_Port: 3306 Source_Log_File: binlog.000030 Read_Source_Log_Pos: 964722776 Relay_Log_File: ip-172-31-88-61-relay-bin.000020 Relay_Log_Pos: 950744511 Relay_Source_Log_File: binlog.000024 Replica_IO_Running: Yes Replica_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: sbtest.sbtest3 Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Error: Exec_Source_Log_Pos: 950744300 Relay_Log_Space: 7407279616 Until_Condition: Source Until_Log_File: binlog.000024 Until_Log_Pos: 950744300 Seconds_Behind_Source: NULL Last_IO_Error: Last_SQL_Error: |
Restore the dump to the target replica where we need to update the filters. The replication SQL thread is paused at the dump’s position, so the restore won’t conflict with any changes the replica is applying.
|
1 2 3 4 |
root@ip-172-31-88-61:/home/ubuntu# myloader --database=sbtest --directory=/tmp/mydump_export --threads=4 -o --queries-per-transaction=5000 -u root -p Percona@1234 root@ip-172-31-88-61:/home/ubuntu# echo $? 0 root@ip-172-31-88-61:/home/ubuntu# |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
mysql> show replica status\G *************************** 1. row *************************** Replica_IO_State: Waiting for source to send event Source_Host: 172.31.92.195 Source_User: repl_user Source_Port: 3306 Source_Log_File: binlog.000031 Read_Source_Log_Pos: 384516500 Relay_Log_File: ip-172-31-88-61-relay-bin.000020 Relay_Log_Pos: 950744511 Relay_Source_Log_File: binlog.000024 Replica_IO_Running: Yes Replica_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: sbtest.sbtest3 Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Error: Exec_Source_Log_Pos: 950744300 Relay_Log_Space: 7900841683 Until_Condition: Source Until_Log_File: binlog.000024 Until_Log_Pos: 950744300 Seconds_Behind_Source: NULL Last_IO_Error: Last_SQL_Error: |
Remove the replication filter so it defaults to replicating all the data from the primary.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
mysql> stop replica; Query OK, 0 rows affected (0.01 sec) mysql> CHANGE REPLICATION FILTER REPLICATE_IGNORE_TABLE = (); Query OK, 0 rows affected (0.00 sec) mysql> show replica status\G *************************** 1. row *************************** Replica_IO_State: Source_Host: 172.31.92.195 Source_User: repl_user Source_Port: 3306 Source_Log_File: binlog.000031 Read_Source_Log_Pos: 391339868 Relay_Log_File: ip-172-31-88-61-relay-bin.000020 Relay_Log_Pos: 950744511 Relay_Source_Log_File: binlog.000024 Replica_IO_Running: No Replica_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Error: Exec_Source_Log_Pos: 950744300 Relay_Log_Space: 7907665051 Until_Condition: Source Until_Log_File: binlog.000024 Until_Log_Pos: 950744300 Seconds_Behind_Source: NULL Last_IO_Error: Last_SQL_Error: |
In this example, there is a single table to remove, so I used empty braces. If multiple tables are added to the replication filter, you may need to remove the specific table while keeping the others.
Example:
|
1 |
CHANGE REPLICATION FILTER REPLICATE_IGNORE_TABLE = (myschema.table1, myschema.table2, myschema.table3); |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
mysql> start replica; Query OK, 0 rows affected (0.03 sec) mysql> show replica status\G *************************** 1. row *************************** Replica_IO_State: Waiting for source to send event Source_Host: 172.31.92.195 Source_User: repl_user Source_Port: 3306 Source_Log_File: binlog.000031 Read_Source_Log_Pos: 416723216 Relay_Log_File: ip-172-31-88-61-relay-bin.000020 Relay_Log_Pos: 953071929 Relay_Source_Log_File: binlog.000024 Replica_IO_Running: Yes Replica_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Error: Exec_Source_Log_Pos: 953071718 Relay_Log_Space: 7933048787 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Seconds_Behind_Source: 3756 Last_IO_Error: Last_SQL_Error: |
Verify that the table is receiving writes from the primary and that there are no replication errors. You can also use pt-table-checksum to verify the data consistency.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
mysql> pager md5sum; PAGER set to 'md5sum' mysql> select * from sbtest.sbtest3; 42b34b3393caab1ad5c897e3dd51452d - 100000 rows in set (0.08 sec) mysql> select * from sbtest.sbtest3; 9db2089cf1c0db893d36a9714f213364 - 100000 rows in set (0.06 sec) mysql> select * from sbtest.sbtest3; 15614dd6f04015a19b8df8c0bff69031 - 100000 rows in set (0.07 sec) mysql> show replica status\G af2a62818210a0ca679f3b29d3eb0832 - 1 row in set (0.00 sec) |
Please note the replication status output above is trimmed to make the blog post short.
This structured approach ensures data consistency while minimising maintenance overhead. I hope the above example gives ideas for how to play with replication filters and helps update MySQL replication in live production without ever touching the primary. Using the above approach, you can add/update multiple tables or schemas in the replication filters. Please comment if you have any questions/comments.
Resources
RELATED POSTS