mysql5.7 主从复制的正常切换【转】

时间:2022-09-21 12:46:24

目前环境如下:

master server IP:172.17.61.131

slave server IP:172.17.61.132

mysql version: mysql-5.7.21-linux

目标:计划内的主从复制正常切换

 

1.切换之前需要检查slave的同步状态,对master进行锁表。

slave server: 确保Slave_IO_Running和Slave_SQL_Running都为YES

[html] view plain copy
  1. mysql> show slave status \G  
  2. *************************** 1. row ***************************  
  3.                Slave_IO_State: Waiting for master to send event  
  4.                   Master_Host: 172.17.61.131  
  5.                   Master_User: repl  
  6.                   Master_Port: 3306  
  7.                 Connect_Retry: 60  
  8.               Master_Log_File: mysql_bin.000036  
  9.           Read_Master_Log_Pos: 154  
  10.                Relay_Log_File: slave_relay_bin.000003  
  11.                 Relay_Log_Pos: 367  
  12.         Relay_Master_Log_File: mysql_bin.000036  
  13.              Slave_IO_Running: Yes  
  14.             Slave_SQL_Running: Yes  
  15.              ...  
  16. 1 row in set (0.00 sec)  

master server进行锁表操作,由于我是测试环境没有应用,所以锁表很快。

2.在flush tables with read lock成功获得锁之前,必须等待所有语句执行完成(包括SELECT)。所以如果有个慢查询在执行,或者一个打开的事务,或者其他进程拿着表锁,flush tables with read lock就会被阻塞,直到所有的锁被释放。

[html] view plain copy
  1. mysql> flush tables with read lock;  
  2. Query OK, 0 rows affected (0.01 sec)  

3.接着要确保master所有的binlog已同步到slave,并且slave都已apply了所有的binlog。

maser server:

[html] view plain copy
  1. mysql> show processlist;  
  2. +----+------+---------------------+------+-------------+------+---------------------------------------------------------------+------------------+  
  3. | Id | User | Host                | db   | Command     | Time | State                                                         | Info             |  
  4. +----+------+---------------------+------+-------------+------+---------------------------------------------------------------+------------------+  
  5. |  2 | repl | 172.17.61.132:60079 | NULL | Binlog Dump | 1907 | Master has sent all binlog to slave; waiting for more updates | NULL             |  
  6. |  3 | root | localhost           | NULL | Query       |    0 | starting                                                      | show processlist |  
  7. +----+------+---------------------+------+-------------+------+---------------------------------------------------------------+------------------+  
  8. 2 rows in set (0.00 sec)  

slave server:

[html] view plain copy
  1. mysql> show processlist;  
  2. +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+  
  3. | Id | User        | Host      | db   | Command | Time | State                                                  | Info             |  
  4. +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+  
  5. |  1 | system user |           | NULL | Connect | 1918 | Slave has read all relay log; waiting for more updates | NULL             |  
  6. |  2 | system user |           | NULL | Connect | 1918 | Waiting for master to send event                       | NULL             |  
  7. |  4 | root        | localhost | NULL | Query   |    0 | starting                                               | show processlist |  
  8. +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+  
  9. 3 rows in set (0.00 sec)  

4.停止slave的进程 

slave sever:

[html] view plain copy
  1. mysql> STOP SLAVE IO_THREAD;  
  2. Query OK, 0 rows affected (0.02 sec)  
  3.   
  4. mysql>  show processlist;  
  5. +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+  
  6. | Id | User        | Host      | db   | Command | Time | State                                                  | Info             |  
  7. +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+  
  8. |  1 | system user |           | NULL | Connect | 2039 | Slave has read all relay log; waiting for more updates | NULL             |  
  9. |  4 | root        | localhost | NULL | Query   |    0 | starting                                               | show processlist |  
  10. +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+  
  11. 2 rows in set (0.00 sec)  

5.修改slave的my.cnf,重启mysql

[html] view plain copy
  1. [mysqld]  
  2. socket = /usr/local/mysql/mysql.sock  
  3. character_set_server= utf8  
  4. init_connect= 'SET NAMES utf8'  
  5. basedir= /usr/local/mysql  
  6. datadir= /u01/mysql  
  7. socket = /u01/mysql/mysql.sock  
  8. log-error= /u01/log/mysql/mysql_3306.err  
  9. pid-file= /u01/mysql/mysqld.pid  
  10. lower_case_table_names = 1  
  11. sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION  
  12. secure-file-priv = /u01/backup  
  13. server-id=10001  
  14. log_bin = /u01/mysql/mysql_bin  
  15. #skip-grant-tables  
  16. innodb_flush_log_at_trx_commit=1  
  17. sync_binlog=1  
  18. #relay-log=/u01/mysql/slave_relay_bin  
  19. expire_logs_days=10  
  20. read_only=0  
  21. #relay_log_recovery=on  
  22. #relay_log_info_repository=TABLE  
  23. max_binlog_size=1073741824  
  24. #autocommit=off  
  25. #long_query_time=15  
  26. #slow_query_log=on  
[html] view plain copy
  1. [root@qht132 ~]# service mysql restart  
  2. Shutting down MySQL..                                      [  OK  ]  
  3. Starting MySQL.                                            [  OK  ]  

需要开启原slave的bin_log,关闭relay_log*,关闭read_only

5.提升slave为master

[html] view plain copy
  1. mysql> stop slave;  
  2. Query OK, 0 rows affected (0.00 sec)  
  3.   
  4. mysql> reset master;  
  5. Query OK, 0 rows affected (0.00 sec)  
  6.   
  7. mysql> reset slave all;  
  8. Query OK, 0 rows affected (0.01 sec)  
  9.   
  10. mysql> show variables like 'read_only';  
  11. +---------------+-------+  
  12. | Variable_name | Value |  
  13. +---------------+-------+  
  14. | read_only     | OFF   |  
  15. +---------------+-------+  
  16. 1 row in set (0.00 sec)  
  17.   
  18. mysql> show master status \G  
  19. *************************** 1. row ***************************  
  20.              File: mysql_bin.000001  
  21.          Position: 154  
  22.      Binlog_Do_DB:  
  23.  Binlog_Ignore_DB:  
  24. Executed_Gtid_Set:  
  25. 1 row in set (0.00 sec)  

在新的master上建立同步帐户并赋予同步权限(由于我为了省事,之前主从配置的时候将61网段的权限都赋予给了repl,所以这一步我也可以不做)

[html] view plain copy
  1. mysql> grant replication slave on *.* to 'repl'@'172.17.61.%';  
  2. Query OK, 0 rows affected (0.01 sec)  

6.修改原master的my.cnf,重启mysql

[html] view plain copy
  1. [mysqld]  
  2. socket = /usr/local/mysql/mysql.sock  
  3. character_set_server= utf8  
  4. init_connect= 'SET NAMES utf8'  
  5. basedir= /usr/local/mysql  
  6. datadir= /u01/mysql  
  7. socket = /u01/mysql/mysql.sock  
  8. log-error= /u01/log/mysql/mysql_3306.err  
  9. pid-file= /u01/mysql/mysqld.pid  
  10. lower_case_table_names = 1  
  11. sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION  
  12. secure-file-priv = /u01/backup  
  13. server-id=10000  
  14. #log_bin = /u01/mysql/mysql_bin  
  15. #skip-grant-tables  
  16. #innodb_flush_log_at_trx_commit=1  
  17. #sync_binlog=1  
  18. expire_logs_days=10  
  19. read_only=1  
  20. relay_log_recovery=on  
  21. relay_log_info_repository=TABLE  
  22. #max_binlog_size=1073741824  
  23. #autocommit=off  
  24. #long_query_time=15  
  25. #slow_query_log=on  

关闭原master的binlog,开启relay_log*,打开read_only状态

[html] view plain copy
  1. [root@qht131 ~]# service mysql restart  
  2. Shutting down MySQL..                                      [  OK  ]  
  3. Starting MySQL.                                            [  OK  ]  

7.将master切换为slave

[html] view plain copy
  1. mysql> reset master;  
  2. Query OK, 0 rows affected (0.00 sec)  
  3.   
  4. mysql> change master to  
  5.     -master_host='172.17.61.132',  
  6.     ->  master_user='repl',  
  7.     ->  master_password='repl',  
  8.     -master_log_file='mysql_bin.000001',  
  9.     -master_log_pos=154;  
  10. Query OK, 0 rows affected, 2 warnings (0.03 sec)  
  11.   
  12. mysql> start slave;  
  13. Query OK, 0 rows affected (0.01 sec)  

master_log_file和master_log_pos需指定为新master切换后显示的起始位置

检查一下新slave的状态,确保slave_io_running和slave_sql_running都为YES。

[html] view plain copy
  1. mysql> show slave status\G  
  2. *************************** 1. row ***************************  
  3.                Slave_IO_State: Waiting for master to send event  
  4.                   Master_Host: 172.17.61.132  
  5.                   Master_User: repl  
  6.                   Master_Port: 3306  
  7.                 Connect_Retry: 60  
  8.               Master_Log_File: mysql_bin.000001  
  9.           Read_Master_Log_Pos: 360  
  10.                Relay_Log_File: qht131-relay-bin.000002  
  11.                 Relay_Log_Pos: 526  
  12.         Relay_Master_Log_File: mysql_bin.000001  
  13.              Slave_IO_Running: Yes  
  14.             Slave_SQL_Running: Yes  
  15.               Replicate_Do_DB:  
  16.           Replicate_Ignore_DB:  
  17.            Replicate_Do_Table:  
  18.        Replicate_Ignore_Table:  
  19.       Replicate_Wild_Do_Table:  
  20.   Replicate_Wild_Ignore_Table:  
  21.                    Last_Errno: 0  
  22.                    Last_Error:  
  23.                  Skip_Counter: 0  
  24.           Exec_Master_Log_Pos: 360  
  25.               Relay_Log_Space: 734  
  26.               Until_Condition: None  
  27.                Until_Log_File:  
  28.                 Until_Log_Pos: 0  
  29.            Master_SSL_Allowed: No  
  30.            Master_SSL_CA_File:  
  31.            Master_SSL_CA_Path:  
  32.               Master_SSL_Cert:  
  33.             Master_SSL_Cipher:  
  34.                Master_SSL_Key:  
  35.         Seconds_Behind_Master: 0  
  36. Master_SSL_Verify_Server_Cert: No  
  37.                 Last_IO_Errno: 0  
  38.                 Last_IO_Error:  
  39.                Last_SQL_Errno: 0  
  40.                Last_SQL_Error:  
  41.   Replicate_Ignore_Server_Ids:  
  42.              Master_Server_Id: 10001  
  43.                   Master_UUID: 744cfcde-3a9b-11e8-b299-000c2900d025  
  44.              Master_Info_File: /u01/mysql/master.info  
  45.                     SQL_Delay: 0  
  46.           SQL_Remaining_Delay: NULL  
  47.       Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates  
  48.            Master_Retry_Count: 86400  
  49.                   Master_Bind:  
  50.       Last_IO_Error_Timestamp:  
  51.      Last_SQL_Error_Timestamp:  
  52.                Master_SSL_Crl:  
  53.            Master_SSL_Crlpath:  
  54.            Retrieved_Gtid_Set:  
  55.             Executed_Gtid_Set:  
  56.                 Auto_Position: 0  
  57.          Replicate_Rewrite_DB:  
  58.                  Channel_Name:  
  59.            Master_TLS_Version:  
  60. 1 row in set (0.00 sec)  

8.最后进行测试

新master:

[html] view plain copy
  1. mysql> create table t3 ( c1 int);  
  2. Query OK, 0 rows affected (0.05 sec)  

新slave:

[html] view plain copy
  1. mysql> use l5m  
  2. Database changed  
  3. mysql> show tables;  
  4. +---------------+  
  5. | Tables_in_l5m |  
  6. +---------------+  
  7. | t1            |  
  8. | t2            |  
  9. | t3            |  
  10. | test_emp      |  
  11. | tt            |  
  12. +---------------+  
  13. 5 rows in set (0.00 sec)  
  14.   
  15. mysql> show create table t3\G  
  16. *************************** 1. row ***************************  
  17.        Table: t3  
  18. Create Table: CREATE TABLE `t3` (  
  19.   `c1` int(11) DEFAULT NULL  
  20. ENGINE=InnoDB DEFAULT CHARSET=utf8  
  21. 1 row in set (0.00 sec)  

至此测试完毕!

 

转自

mysql5.7 主从复制的正常切换 - CSDN博客

https://blog.csdn.net/jolly10/article/details/79877564