MySQL Master-Slave Switch: Step-by-Step
MySQL master-slave switch is a high availability solution for databases, which ensures that in case of failure in the master database, the slave database can take over and continue providing services. The steps for the switch are as follows:
- Confirming the failure of the primary database: First, it is necessary to confirm that the primary database has experienced a failure and is unable to provide service.
- Manual Switching: This refers to the administrator manually performing a switch operation, promoting the standby database to become the primary database. During manual switching, the following steps need to be conducted:
- Stop the replication process from the database.
- Stop the slave replication by executing the STOP SLAVE command on the database.
- To enable read-write functionality on the database, execute the command SET GLOBAL read_only=OFF;
- Executing the RESET MASTER command on the database will clear the binary logs.
- Stop the MySQL service on the main database.
- Switch the data directory from the database to the main database.
- Start the MySQL service.
- Automatic failover is the process where the system automatically promotes a standby database to the primary database when the primary database fails. This process relies on high availability software such as Pacemaker and Corosync, which monitor the status of the primary database and initiate the failover process when needed.
Regardless of whether it is manually or automatically switched, it is necessary to perform some essential checks and tests on the database after switching to ensure a successful switch and proper system operation.