Handling MySQL Replication Data Drift: Mastering Percona Toolkit with Zero Downtime

MySQL tutorial - IT technology blog
MySQL tutorial - IT technology blog

When Replication Shows “Green” but Data is Wrong

At 2 AM, Slack notifications start firing non-stop. The monitoring system reports Data Drift: Report data on the Slave is completely different from the Master, even though <a href="https://itfromzero.com/en/mysql-en/solving-mysql-replication-lag-speed-up-syncing-with-parallel-replication.html">Seconds_Behind_Master</a> is still 0. This is an incredibly tricky situation. Replication is still running, but the contents of the two servers are completely out of sync.

Ops engineers are no strangers to this scenario. Perhaps someone accidentally overwrote data directly on the Slave. Or sometimes a rare bug causes transactions to replicate inconsistently. The question is: How do you know exactly which table is drifting without having to dump/restore hundreds of GBs of data?

Why Traditional Methods Often Fail

Usually, when data drift is detected, we tend to think of two options:

  • Using mysqldump: Dump from the Master and restore to the Slave. With a database around 500GB, locking tables for the dump and transferring it over the network is a disaster. Your system might face downtime for hours.
  • Comparing COUNT(*): This method only counts rows. It is completely useless if the row counts match but the internal content (such as prices or order statuses) is incorrect.

This is where Percona Toolkit, with the duo pt-table-checksum and pt-table-sync, becomes a real lifesaver. It helps handle data drift while keeping the system online 24/7.

The Intelligent Mechanism of Percona Toolkit

pt-table-checksum does not pull data to a local machine for comparison. Instead, it breaks tables into chunks and runs SQL commands directly on the Master to calculate checksums. Thanks to the replication mechanism, these commands automatically execute on the Slave. Finally, the tool simply compares the checksum results between the two sides to pinpoint which table is inconsistent.

Quick Installation

On Ubuntu/Debian, you only need one command:

sudo apt-get install percona-toolkit

For CentOS/RHEL, use yum:

sudo yum install percona-toolkit

Step 1: Tracking Down Inconsistent Tables with pt-table-checksum

Before starting, create a percona_user with SUPER and REPLICATION CLIENT privileges. This user needs full permissions on the percona database to store temporary results.

Run the check command as follows:

pt-table-checksum h=master_ip,u=percona_user,p=password \
    --databases=my_production_db \
    --replicate=percona.checksums \
    --create-replicate-table \
    --no-check-replication-filters

Quick Explanation:

  • --replicate: Save results to the percona.checksums table.
  • --create-replicate-table: Automatically initialize the table if it doesn’t exist.
  • --no-check-replication-filters: Force the tool to check tables even if they are filtered in the MySQL config.

After running, look at the DIFFS column. If this number is greater than 0, that table definitely has data drift.

Pro tip: For tables with over 10 million rows, I always add --max-load Threads_running=25. If the server is overloaded, the tool will automatically pause to protect the system.

Step 2: Synchronizing Data with pt-table-sync

Once the faulty table is identified (e.g., orders), it’s time to fix it. pt-table-sync will automatically generate REPLACE or DELETE commands to bring the Slave back to the same state as the Master.

Don’t execute it immediately. Run “dry run” mode first to see what the tool intends to fix:

pt-table-sync --print \
    --replicate=percona.checksums \
    h=master_ip,u=percona_user,p=password \
    h=slave_ip

If the displayed SQL commands look correct, run the execution command:

pt-table-sync --execute \
    --replicate=percona.checksums \
    h=master_ip,u=percona_user,p=password \
    h=slave_ip

Note: This process writes data directly to the Slave. If the table is too large, closely monitor the server’s I/O and load.

Common Pitfalls to Avoid on Production

After handling large databases many times, I’ve gathered 4 important notes:

  1. Primary Key is Mandatory: The tool operates based on Indexes. If a table lacks a PK, it will perform a Full Table Scan, causing heavy lag and severe table locking.
  2. Network Latency: Although it only sends checksums, if the Master and Slave are in different Regions (e.g., Singapore and the US), the process will still take time.
  3. Be Careful with Triggers: If the Slave has triggers that automatically update other tables, syncing might cause unwanted domino effects.
  4. Write Permissions: Ensure the user performing the sync has SUPER privileges to bypass read_only = 1 mode on the Slave.

Conclusion

MySQL data drift is something you will eventually encounter in long-term operations. Instead of spending all day on dump/restore, mastering Percona Toolkit will help you handle the problem cleanly and professionally. Next time you’re woken up in the middle of the night, stay calm and run the commands—your data will be back in sync in no time.

Share: