Rate This Document
Findability
Accuracy
Completeness
Readability

Performing a Normal Switchover

  1. Stop the client application, that is, stop the client's operations on the primary database.
  2. Ensure that replication is complete on all secondary databases. For details, see 2.
  3. Obtain the status values of the primary database. For details, see 3.
  4. Obtain the status values of the secondary databases. For details, see 4.
  5. Compare the obtained status values of the primary database and secondary databases to check whether all relay logs have been replayed in the secondary databases. For details, see 5.
  6. Switch secondary database 1 as the primary database.
    1. Log in to secondary database 1.
      1
      /usr/local/mysql/bin/mysql -uroot -p123456 -S /data/mysql/run/mysql.sock
      
    2. Switch secondary database 1 as the primary database.

      Stop the secondary database, delete all information about the secondary database, and query the status of the secondary database.

      stop slave;
      reset slave all;
      show slave status \G;

      Query the status of the primary database and record the values of File (binlog file name) and Position (latest position in the binlog). These two parameters will be used in 8.4.

      show master status;

      To reset File (binlog file name) and Position (latest position in the binlog) for the primary database, run the following commands:

      reset master;
      show master status;
    3. Ensure that the log_bin value of the new primary database (secondary database 1) is ON.
      show variables like 'log_bin';

      If the value of log_bin is OFF, perform the following operations:

      1. Open the configuration file.
        1
        vim /etc/my.cnf
        
      2. Press i to enter the insert mode and modify the file as follows:
        [mysqld]
        log-bin=mysql-bin
      3. Press Esc, type :wq!, and press Enter to save the file and exit.
      4. Restart the MySQL service.
        1
        service mysql restart
        

        Or

        1
        2
        /usr/local/mysql/bin/mysqladmin -uroot -p123456 shutdown -S /data/mysql/run/mysql.sock
        /usr/local/mysql/bin/mysqld  --defaults-file=/etc/my.cnf &
        
  7. Configure secondary database 2 to point to the new primary database.
    1. Log in to secondary database 2.
      1
      /usr/local/mysql/bin/mysql -uroot -p123456 -S /data/mysql/run/mysql.sock
      
    2. Stop the secondary database, delete all information about the secondary database, and query the status of the secondary database.
      stop slave;
      reset slave all;
      show slave status \G;
    3. Configure the replication mode. For details, see 4.
    4. Configure secondary database 2 to point to the new primary database based on the replication mode configured in 3.
      • Traditional mode (The values of master_log_file and master_log_pos are obtained in 6.b. Replace them based on your requirements):
        change master to master_host='192.168.220.72',master_port=3306,master_user='replicate',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=155;
      • GTID mode:
        change master to master_host='192.168.220.72',master_port=3306,master_user='replicate',master_password='123456',master_auto_position=1;
    5. Start the secondary database.
      start slave;
    6. Query the status of the secondary database.
      show slave status \G;

      If the values of the following two parameters are Yes, the secondary database is configured successfully.

      Slave_IO_Running: Yes

      Slave_SQL_Running: Yes

  8. Configure the original primary database to point to the new primary database.
    1. Log in to the original primary database, which is already a secondary database.
      1
      /usr/local/mysql/bin/mysql -uroot -p123456 -S /data/mysql/run/mysql.sock
      
    2. Stop the secondary database, delete all information about the secondary database, and query the status of the secondary database.
      stop slave;
      reset slave all;
      show slave status \G;
    3. Configure the replication mode. For details, see 4.
    4. Configure the original primary database to point to the new primary database based on the replication mode configured in 3.
      • Traditional mode (The values of master_log_file and master_log_pos are obtained in 6.b. Replace them based on your requirements):
        change master to master_host='192.168.220.72',master_port=3306,master_user='replicate',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=155;
      • GTID mode:
        change master to master_host='192.168.220.72',master_port=3306,master_user='replicate',master_password='123456',master_auto_position=1;
    5. Start the secondary database.
      start slave;
    6. Query the status of the secondary database.
      show slave status \G;

      If the values of the following two parameters are Yes, the secondary database is configured successfully.

      Slave_IO_Running: Yes

      Slave_SQL_Running: Yes