How to swap a replication and a master database with each other efficiently

On the master database

1. set the master database on read only

SET GLOBAL super_read_only = ON;
SET GLOBAL read_only = ON;

On slave database

find the last bin log position and file to later make this the replica

SHOW MASTER STATUS;

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.011112 |  5895164 | emplemdb     |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

Save this output to a text file

stop the slave status and check the read only status. It should stop being read only

STOP SLAVE;

RESET SLAVE ALL;

Now this is your new master database

Change the app credentials to look at the slave database
On the old server setup the slave status and start slave

CHANGE REPLICATION SOURCE TO
  SOURCE_HOST='PRIMARY_IP',
  SOURCE_USER='repl',
  SOURCE_PASSWORD='StrongPassword',
  SOURCE_LOG_FILE='mysql-bin.000012',
  SOURCE_LOG_POS=45678;

start slave;


Revision #3
Created 2025-12-12 11:25:28 UTC by Admin
Updated 2025-12-12 20:26:39 UTC by Admin