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;
2. 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
On slave database
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