snippetsqlMinor
How do I switch master with slave and vice versa on MySQL?
Viewed 0 times
vicewithslaveversamysqlmasterhowandswitch
Problem
I have two instances of MySQL, one master and one slave.
How can I switch them to make the slave the master and vice-versa while avoiding downtime?
How can I switch them to make the slave the master and vice-versa while avoiding downtime?
Solution
If you use a DBVIP, you could remove the DBVIP from the Master and bring it up on the Slave.
EXAMPLE
Let's make up a DBVIP, like
Put this up on the Master in the OS
Replace your IP in your application with
Here is something aggressive:
If you see DB Connections coming in from the Apache Servers, CONGRATULATIONS you have manually performed a failover.
UPDATE 2012-09-19 14:28 EDT
If you cannot use a DBVIP, you must do more work instead
STEP 01) Activate Binary Logging on the Slave
Add this to /etc/my.cnf on the Slave
STEP02)
Binary Logging should be enabled in the Slave
STEP03) Run the
STEP04) Run
At this point
This configuration is better known as
STEP05)
STEP06) Change the IP address in the App to Connect to the Slave
STEP07)
STEP08)
If you see DB Connections coming in from the Apache Servers, CONGRATULATIONS you have manually performed a failover without MySQL Downtime. The only downtime there is comes from the Window of time STEP05 - STEP07.
EXAMPLE
Let's make up a DBVIP, like
10.1.2.30.Put this up on the Master in the OS
ip addr addr 10.1.2.30/24 dev eth1Replace your IP in your application with
10.1.2.30, Let the application continue to run for a while, making sure the DBVIP is being used by the application.Here is something aggressive:
service httpd stopon all Apache Servers
service mysql stopon the Master
ip addr del 10.1.2.30/24 dev eth1on the Master
ip addr add 10.1.2.30/24 dev eth1on the Slave
service httpd starton all Apache Servers
SHOW PROCESSLIST;on the Slave to make sure there are incoming DB Connections
If you see DB Connections coming in from the Apache Servers, CONGRATULATIONS you have manually performed a failover.
UPDATE 2012-09-19 14:28 EDT
If you cannot use a DBVIP, you must do more work instead
STEP 01) Activate Binary Logging on the Slave
Add this to /etc/my.cnf on the Slave
[mysqld]
log-bin=mysql-binSTEP02)
service mysql restart on the SlaveBinary Logging should be enabled in the Slave
STEP03) Run the
CHANGE MASTER TO command on the Master using the Slave as its Master- Use mysql-bin.000001 as the Master_Log_File
- Use the following number as the Master_Log_Pos
- 107 for MySQL 5.5
- 106 for MySQL 5.1
- 98 for MySQL 5.0
STEP04) Run
START SLAVE; on the MasterAt this point
- the Master is the Slave's Slave
- the Slave is the Master Slave
This configuration is better known as
- Master/Master
- Circular Replication
STEP05)
service httpd stop on all Apache ServersSTEP06) Change the IP address in the App to Connect to the Slave
STEP07)
service httpd start on all Apache ServersSTEP08)
SHOW PROCESSLIST; on the Slave to make sure there are incoming DB ConnectionsIf you see DB Connections coming in from the Apache Servers, CONGRATULATIONS you have manually performed a failover without MySQL Downtime. The only downtime there is comes from the Window of time STEP05 - STEP07.
Code Snippets
ip addr addr 10.1.2.30/24 dev eth1[mysqld]
log-bin=mysql-binContext
StackExchange Database Administrators Q#24549, answer score: 5
Revisions (0)
No revisions yet.