目前环境如下:
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
- mysql> show slave status \G
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 172.17.61.131
- Master_User: repl
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: mysql_bin.000036
- Read_Master_Log_Pos: 154
- Relay_Log_File: slave_relay_bin.000003
- Relay_Log_Pos: 367
- Relay_Master_Log_File: mysql_bin.000036
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
- ...
- 1 row in set (0.00 sec)
master server进行锁表操作,由于我是测试环境没有应用,所以锁表很快。
2.在flush tables with read lock成功获得锁之前,必须等待所有语句执行完成(包括SELECT)。所以如果有个慢查询在执行,或者一个打开的事务,或者其他进程拿着表锁,flush tables with read lock就会被阻塞,直到所有的锁被释放。
- mysql> flush tables with read lock;
- Query OK, 0 rows affected (0.01 sec)
3.接着要确保master所有的binlog已同步到slave,并且slave都已apply了所有的binlog。
maser server:
- mysql> show processlist;
- +----+------+---------------------+------+-------------+------+---------------------------------------------------------------+------------------+
- | Id | User | Host | db | Command | Time | State | Info |
- +----+------+---------------------+------+-------------+------+---------------------------------------------------------------+------------------+
- | 2 | repl | 172.17.61.132:60079 | NULL | Binlog Dump | 1907 | Master has sent all binlog to slave; waiting for more updates | NULL |
- | 3 | root | localhost | NULL | Query | 0 | starting | show processlist |
- +----+------+---------------------+------+-------------+------+---------------------------------------------------------------+------------------+
- 2 rows in set (0.00 sec)
slave server:
- mysql> show processlist;
- +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
- | Id | User | Host | db | Command | Time | State | Info |
- +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
- | 1 | system user | | NULL | Connect | 1918 | Slave has read all relay log; waiting for more updates | NULL |
- | 2 | system user | | NULL | Connect | 1918 | Waiting for master to send event | NULL |
- | 4 | root | localhost | NULL | Query | 0 | starting | show processlist |
- +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
- 3 rows in set (0.00 sec)
4.停止slave的进程
slave sever:
- mysql> STOP SLAVE IO_THREAD;
- Query OK, 0 rows affected (0.02 sec)
- mysql> show processlist;
- +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
- | Id | User | Host | db | Command | Time | State | Info |
- +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
- | 1 | system user | | NULL | Connect | 2039 | Slave has read all relay log; waiting for more updates | NULL |
- | 4 | root | localhost | NULL | Query | 0 | starting | show processlist |
- +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
- 2 rows in set (0.00 sec)
5.修改slave的my.cnf,重启mysql
- [mysqld]
- socket = /usr/local/mysql/mysql.sock
- character_set_server= utf8
- init_connect= 'SET NAMES utf8'
- basedir= /usr/local/mysql
- datadir= /u01/mysql
- socket = /u01/mysql/mysql.sock
- log-error= /u01/log/mysql/mysql_3306.err
- pid-file= /u01/mysql/mysqld.pid
- lower_case_table_names = 1
- sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
- secure-file-priv = /u01/backup
- server-id=10001
- log_bin = /u01/mysql/mysql_bin
- #skip-grant-tables
- innodb_flush_log_at_trx_commit=1
- sync_binlog=1
- #relay-log=/u01/mysql/slave_relay_bin
- expire_logs_days=10
- read_only=0
- #relay_log_recovery=on
- #relay_log_info_repository=TABLE
- max_binlog_size=1073741824
- #autocommit=off
- #long_query_time=15
- #slow_query_log=on
- [root@qht132 ~]# service mysql restart
- Shutting down MySQL.. [ OK ]
- Starting MySQL. [ OK ]
需要开启原slave的bin_log,关闭relay_log*,关闭read_only
5.提升slave为master
- mysql> stop slave;
- Query OK, 0 rows affected (0.00 sec)
- mysql> reset master;
- Query OK, 0 rows affected (0.00 sec)
- mysql> reset slave all;
- Query OK, 0 rows affected (0.01 sec)
- mysql> show variables like 'read_only';
- +---------------+-------+
- | Variable_name | Value |
- +---------------+-------+
- | read_only | OFF |
- +---------------+-------+
- 1 row in set (0.00 sec)
- mysql> show master status \G
- *************************** 1. row ***************************
- File: mysql_bin.000001
- Position: 154
- Binlog_Do_DB:
- Binlog_Ignore_DB:
- Executed_Gtid_Set:
- 1 row in set (0.00 sec)
在新的master上建立同步帐户并赋予同步权限(由于我为了省事,之前主从配置的时候将61网段的权限都赋予给了repl,所以这一步我也可以不做)
- mysql> grant replication slave on *.* to 'repl'@'172.17.61.%';
- Query OK, 0 rows affected (0.01 sec)
6.修改原master的my.cnf,重启mysql
- [mysqld]
- socket = /usr/local/mysql/mysql.sock
- character_set_server= utf8
- init_connect= 'SET NAMES utf8'
- basedir= /usr/local/mysql
- datadir= /u01/mysql
- socket = /u01/mysql/mysql.sock
- log-error= /u01/log/mysql/mysql_3306.err
- pid-file= /u01/mysql/mysqld.pid
- lower_case_table_names = 1
- sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
- secure-file-priv = /u01/backup
- server-id=10000
- #log_bin = /u01/mysql/mysql_bin
- #skip-grant-tables
- #innodb_flush_log_at_trx_commit=1
- #sync_binlog=1
- expire_logs_days=10
- read_only=1
- relay_log_recovery=on
- relay_log_info_repository=TABLE
- #max_binlog_size=1073741824
- #autocommit=off
- #long_query_time=15
- #slow_query_log=on
关闭原master的binlog,开启relay_log*,打开read_only状态
- [root@qht131 ~]# service mysql restart
- Shutting down MySQL.. [ OK ]
- Starting MySQL. [ OK ]
7.将master切换为slave
- mysql> reset master;
- Query OK, 0 rows affected (0.00 sec)
- mysql> change master to
- -> master_host='172.17.61.132',
- -> master_user='repl',
- -> master_password='repl',
- -> master_log_file='mysql_bin.000001',
- -> master_log_pos=154;
- Query OK, 0 rows affected, 2 warnings (0.03 sec)
- mysql> start slave;
- Query OK, 0 rows affected (0.01 sec)
master_log_file和master_log_pos需指定为新master切换后显示的起始位置
检查一下新slave的状态,确保slave_io_running和slave_sql_running都为YES。
- mysql> show slave status\G
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 172.17.61.132
- Master_User: repl
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: mysql_bin.000001
- Read_Master_Log_Pos: 360
- Relay_Log_File: qht131-relay-bin.000002
- Relay_Log_Pos: 526
- Relay_Master_Log_File: mysql_bin.000001
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
- Replicate_Do_DB:
- Replicate_Ignore_DB:
- Replicate_Do_Table:
- Replicate_Ignore_Table:
- Replicate_Wild_Do_Table:
- Replicate_Wild_Ignore_Table:
- Last_Errno: 0
- Last_Error:
- Skip_Counter: 0
- Exec_Master_Log_Pos: 360
- Relay_Log_Space: 734
- Until_Condition: None
- Until_Log_File:
- Until_Log_Pos: 0
- Master_SSL_Allowed: No
- Master_SSL_CA_File:
- Master_SSL_CA_Path:
- Master_SSL_Cert:
- Master_SSL_Cipher:
- Master_SSL_Key:
- Seconds_Behind_Master: 0
- Master_SSL_Verify_Server_Cert: No
- Last_IO_Errno: 0
- Last_IO_Error:
- Last_SQL_Errno: 0
- Last_SQL_Error:
- Replicate_Ignore_Server_Ids:
- Master_Server_Id: 10001
- Master_UUID: 744cfcde-3a9b-11e8-b299-000c2900d025
- Master_Info_File: /u01/mysql/master.info
- SQL_Delay: 0
- SQL_Remaining_Delay: NULL
- Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
- Master_Retry_Count: 86400
- Master_Bind:
- Last_IO_Error_Timestamp:
- Last_SQL_Error_Timestamp:
- Master_SSL_Crl:
- Master_SSL_Crlpath:
- Retrieved_Gtid_Set:
- Executed_Gtid_Set:
- Auto_Position: 0
- Replicate_Rewrite_DB:
- Channel_Name:
- Master_TLS_Version:
- 1 row in set (0.00 sec)
8.最后进行测试
新master:
- mysql> create table t3 ( c1 int);
- Query OK, 0 rows affected (0.05 sec)
新slave:
- mysql> use l5m
- Database changed
- mysql> show tables;
- +---------------+
- | Tables_in_l5m |
- +---------------+
- | t1 |
- | t2 |
- | t3 |
- | test_emp |
- | tt |
- +---------------+
- 5 rows in set (0.00 sec)
- mysql> show create table t3\G
- *************************** 1. row ***************************
- Table: t3
- Create Table: CREATE TABLE `t3` (
- `c1` int(11) DEFAULT NULL
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8
- 1 row in set (0.00 sec)
至此测试完毕!
转自
mysql5.7 主从复制的正常切换 - CSDN博客
https://blog.csdn.net/jolly10/article/details/79877564