How to re-sync the Mysql DB if Master and slave have different database incase of Mysql replication?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Understanding MySQL Replication and Divergence
MySQL replication is a powerful feature used to maintain a consistent copy of the database, ensuring redundancy and enabling scaling by distributing read queries across multiple servers. This system typically follows a master-slave architecture where the master database processes all writes and updates, and these changes are subsequently copied to one or more slave databases.
However, replication can sometimes get out of sync due to various reasons such as network issues, server crashes, or manual data modifications. When this happens, the databases on the master and the slave may differ, which could lead to inconsistent data read operations and other failures.
To re-sync the MySQL databases effectively, certain procedures must be followed to restore consistency.
Prerequisites
Before proceeding, ensure the following:
- Adequate backup of both master and slave databases.
- Full understanding of your application's behavior and data requirements.
- Necessary privileges to access both master and slave servers.
Steps to Re-Sync Master and Slave Databases
To re-sync a MySQL replication setup, consider the following detailed steps:
1. Check Replication Status
Begin by checking the replication status on the slave server. You can determine whether replication is stopped and know at which point data divergence might have occurred.
- `Slave_IO_Running`: Indicates whether the slave I/O thread is running.
- `Slave_SQL_Running`: Indicates whether the slave SQL thread is running.
- `Seconds_Behind_Master`: A value that shows how far the slave is behind (useful for initial examination).
- Error Handling: Keep an eye on `Last_SQL_Error` or `Last_IO_Error` for any discrepancies identified in the slave status that could indicate failure points.
- Consistent Snapshots: Use `FLUSH TABLES WITH READ LOCK` on the master prior to backup to ensure transactional consistency.
- Network Considerations: Persistent network instability may require alternate arrangements for data consistency checks, such as periodic differencing tools.

