Primary/Secondary Switchover
After the data consistency verification is complete and all data in the secondary database is correct, perform a primary/secondary switchover to adjust the existing primary/secondary replication structure. After the switchover is complete, check the consistency between the new primary database and the new secondary database.
- Before the primary/secondary switchover, there is one primary database and two secondary databases. The primary database and secondary database 1 are on x86 servers, and the secondary database 2 is on a Kunpeng server.
- Switch the Kunpeng secondary database to the new primary database. The original x86 primary database turns into a new secondary database
The primary/secondary switchover is shown in Figure 1.
The following describes how to perform a primary/secondary switchover. After the switchover, the new secondary database can successfully connect to and keep data consistent with the new Kunpeng primary database.
- Upgrade secondary database 2 (Kunpeng) to the new primary database.
- Log in to the Kunpeng server (running the original secondary database 2).
- Stop the secondary database service and delete all secondary database configurations to upgrade it to the primary database.
stop slave; reset slave all;
- Ensure that the value of log_bin of the new primary database is ON.
show variables like 'log_bin';
If the value of log_bin is OFF, perform the following operations to enable log_bin.- Open the /etc/my.cnf file.
vim /etc/my.cnf
- Press i to enter the insert mode and find the [mysqld] section in the file. If this section does not exist, add it. Modify the [mysqld] section as follows:
log-error=/data/mysql/log/mysql.log pid-file=/data/mysql/run/mysqld.pid [mysqldump] quick [mysql] no-auto-rehash [client] default-character-set=utf8 [mysqld] basedir=/usr/local/mysql socket=/data/mysql/run/mysql.sock tmpdir=/data/mysql/tmp datadir=/data/mysql/data default_authentication_plugin=caching_sha2_password port=3306 user=mysql server-id=3 # The server-id of the new primary database server is 3. log-bin=/data/mysql/data/mysql-bin relay_log=/data/mysql/relaylog/relay-bin report_host=192.168.0.3 # 192.168.0.3 is the IP address of the new primary database server. Change it based on the actual IP address.
- Press Esc, type :wq!, and press Enter to save the file and exit.
- Restart MySQL to make the configurations take effect. See 3 for the restart commands.
- Open the /etc/my.cnf file.
- Connect secondary database 1 (x86) to the new primary database.
- Log in to secondary database 1 on the x86 server.
- Stop the secondary database service and delete all secondary database configurations.
stop slave; reset slave all;
- Connect secondary database 1 (x86) to the new primary database.
change master to master_host='192.168.0.3',master_port=3306,master_user='replicate',master_password='123456',master_auto_position=1, get_master_public_key=1;
192.168.0.3 is the IP address of the new primary database server. Change it based on the actual IP address.
- Start and verify the status of the secondary database.
start slave; show slave status \G;
If the values of Slave_IO_Running and Slave_SQL_Running are both Yes, secondary database 1 has been successfully connected to the new primary database.

- Connect the original primary database to the new primary database.
- Log in to the new secondary database (which is the original primary database).
- Stop the secondary database service and delete all secondary database configurations.
stop slave; reset slave all;
- Connect the new secondary database to the new primary database.
change master to master_host='192.168.0.3',master_port=3306,master_user='replicate',master_password='123456',master_auto_position=1, get_master_public_key=1;
192.168.0.3 is the IP address of the new primary database server. Change it based on the actual IP address.
- Start and verify the status of the secondary database.
start slave; show slave status \G;
If the values of Slave_IO_Running and Slave_SQL_Running are both Yes, the new secondary database has been successfully connected to the new primary database.

- Verify the primary/secondary consistency again.
pt-table-checksum h=192.168.0.3,u=checksums,p=123456,P=3306 --databases=tpcc --no-check-binlog-format --replicate=pt.checksums --max-lag=3 --recursion-method=hosts
192.168.0.3 is the IP address of the new primary database server. Change it based on the actual IP address.
