Switch Master and Slave role in mysql
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In MySQL replication, the terms "Master" and "Slave" (more recently referred to as "Primary" and "Replica") are used to describe the relationship between databases in a replication setup. The Master database acts as the primary source of data, while the Slave is a copy that receives updates from the Master asynchronously. In some scenarios, you may need to promote a Slave to a Master, or the other way around, due to hardware failures, load balancing, maintenance, or planned upgrades.
This article explores the technical aspects of switching roles between Master and Slave databases in a MySQL replication environment.
Concepts of Switching Roles
Master Role
The Master server handles active data transactions and writes. It logs all changes to its binary logs, which can then be applied to the Slave databases. The Master server is responsible for:
- Handling all writes (INSERT, UPDATE, DELETE).
- Managing the binary log which tracks changes.
- Providing the binary log information to Slave servers.
Slave Role
The Slave server acts as a backup and a repository for distributed read operations. It synchronizes its data with the Master:
- Pulls changes from the Master's binary log.
- Applies these changes locally to maintain data consistency.
- Can be used for read-only queries to offload the Master.
Switching Roles Scenarios
Scenario 1: Promoting a Slave to a Master
When a Master server fails or needs to be replaced, promoting a Slave to a Master is necessary. This involves:
- Stopping Replication: Cease any ongoing replication processes on the Slave.
- Ensuring Consistency: Verify that the Slave is fully up to date with the Master. The
SHOW SLAVE STATUS \G;command can confirm this. - Promoting the Slave:
- Disable read-only mode on the Slave (if enabled).
- Restart the server as a master instance.
- Reconfiguring Other Slaves: Adjust the Slave configurations of any other servers that were connected to the old Master to now connect to the new Master.
Scenario 2: Demoting a Master to a Slave
If a Slave is more powerful, you might want to demote a Master to a Slave. Positions need to be synchronized, and binary logs reassessed:
- Sync the Slave up to the Master: Make sure the fallback Slave has all the logs from the current Master.
- Stop Writes to Master: Temporarily halt operations on the Master to stabilize data states.
- Change Master to New Master: Use the following to start replication from the new Master:
- Start Slave on Old Master:
Technical Example
Below is a practical example of switching a Slave to become a Master.
Table Summary
| Action | Command(s) |
| Stop Slave process | STOP SLAVE; |
| Disable read-only mode | SET GLOBAL read_only = OFF; |
| Configure new Master | CHANGE MASTER TO MASTER_HOST='new_master_host', ... |
| Start Slave on old Master | START SLAVE; |
| Check Slave status | SHOW SLAVE STATUS \G; |
Additional Considerations
- Consistency: Always verify transaction consistency before promoting or demoting roles. It ensures there’s no data loss.
- Network Latencies: Switching roles can affect network load. Account for these changes in your load balancing strategy.
- Backup and Recovery: Regular backups are crucial before any role transition to prevent data loss.
- Session Configuration: After changing roles, ensure all session-based configurations are revisited for accuracy.
In conclusion, role reversal in a MySQL replication setup is a crucial process that allows database administrators to maintain availability and performance, especially during unexpected outages or planned upgrades. The adaptable nature of MySQL replication architectures makes it possible to achieve high performance and availability when role transition processes are well-understood and implemented.

