MySQL主从切换

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:
1、切换之前确保主从是同步的 
原主库(192.168.10.197): 
mysql> show processlist; 
+----+-------------+----------------------+------+-------------+------+-----------------------------------------------------------------------------+------------------+ 
| Id | User | Host | db | Command | Time | State | Info | 
+----+-------------+----------------------+------+-------------+------+-----------------------------------------------------------------------------+------------------+ 
| 2 | system user | | NULL | Connect | 1348 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL | 
| 16 | repli | 192.168.10.226:50357 | NULL | Binlog Dump | 141 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL | 
| 17 | root | localhost | NULL | Query | 0 | init | show processlist | 
+----+-------------+----------------------+------+-------------+------+-----------------------------------------------------------------------------+------------------+ 
3 rows in set (0.00 sec) 
mysql> show master status\G 
*************************** 1. row *************************** 
File: mysql-bin.000009 
Position: 319 
Binlog_Do_DB: 
Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec) 
原从库(192.168.10.226): 
mysql> show processlist; 
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+ 
| Id | User | Host | db | Command | Time | State | Info | 
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+ 
| 14 | root | localhost | NULL | Query | 0 | init | show processlist | 
| 15 | system user | | NULL | Connect | 224 | Waiting for master to send event | NULL | 
| 16 | system user | | NULL | Connect | 971 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL | 
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+ 
3 rows in set (0.03 sec) 
mysql>show slave status \G 
*************************** 1. row *************************** 
Slave_IO_State: Waiting for master to send event 
Master_Host: 192.168.10.197 
Master_User: repli 
Master_Port: 3306 
Connect_Retry: 60 
Master_Log_File: mysql-bin.000009 
Read_Master_Log_Pos: 319 
....... 
Slave_IO_Running: Yes 
Slave_SQL_Running: Yes 
...... 
Seconds_Behind_Master: 0  

2、修改参数  vi /etc/my.cnf 
原主库: 
read-only=1 
log_slave_updates=1 
relay-log=/var/lib/mysql/mysql-relay-bin 
relay-log-index=/var/lib/mysql/mysql-relay-bin.index 
skip-slave-start=1 
innodb_flush_log_at_trx_commit = 1 
sync-binlog=1 

原备库: 
#read-only=1    ---注释掉只读模式 

3、操作原从库 
mysql> stop slave io_thread; 
Query OK, 0 rows affected (0.11 sec) 
mysql> show processlist; 
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+ 
| Id | User | Host | db | Command | Time | State | Info | 
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+ 
| 16 | system user | | NULL | Connect | 9728 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL | 
| 17 | root | localhost | NULL | Query | 0 | init | show processlist | 
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+ 
2 rows in set (0.05 sec) 
mysql> show slave status \G 
..... 
Slave_IO_Running: No     ---这个已停掉 
Slave_SQL_Running: Yes 
原从库变新主库 
mysql> stop slave; 
Query OK, 0 rows affected (0.04 sec) 
mysql> reset master; 
Query OK, 0 rows affected (2.26 sec) 
mysql> reset slave; 
Query OK, 0 rows affected (0.04 sec) 
mysql> show master status\G 
*************************** 1. row *************************** 
File: mysql-bin.000001 
Position: 120 
Binlog_Do_DB: 
Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.02 sec) 

4、操作原主库 
mysql> reset master; 
Query OK, 0 rows affected (0.15 sec) 
mysql> reset slave; 
ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first 
mysql> stop slave; 
Query OK, 0 rows affected (0.07 sec) 
mysql> reset slave; 
Query OK, 0 rows affected (0.09 sec) 
原主库变新从库 
mysql> CHANGE MASTER TO 
-> MASTER_HOST='192.168.10.226', 
-> MASTER_USER='repli', 
-> MASTER_PASSWORD='repli', 
-> MASTER_LOG_FILE='mysql-bin.000001', 
-> MASTER_LOG_POS=120; 
Query OK, 0 rows affected, 2 warnings (0.05 sec) 
mysql> start slave; 
Query OK, 0 rows affected (0.13 sec) 
mysql> show slave status \G 
*************************** 1. row *************************** 
Slave_IO_State: Waiting for master to send event 
Master_Host: 192.168.10.226 
Master_User: repli 
Master_Port: 3306 
Connect_Retry: 60 
Master_Log_File: mysql-bin.000001 
Read_Master_Log_Pos: 120 
Relay_Log_File: localhost-relay-bin.000002 
Relay_Log_Pos: 283 
Relay_Master_Log_File: mysql-bin.000001 
Slave_IO_Running: Yes 
Slave_SQL_Running: Yes 

5、删除新从库上的relay-log.info 文件,否则下次重启slave时会报如下错误。 
/var/lib/mysql 
-rw-rw---- 1 mysql mysql 127 Jan 5 22:31 master.info 
-rw-rw---- 1 mysql mysql 62 Jan 5 22:31 relay-log.info 
mv  relay-log.info  /tmp 
mysql> start slave; 
ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository 
由于新的slave改变了服务端口和文件路径,分析应该是由于mysql-relay-bin.index中仍然保存着旧relay日志文件的路径,而这些路径下又找不到合适的文件,因此报错。 

出现这个这个问题解决方法: 
先删掉文件,再重启mysql服务:service mysql restart 
mysql> start slave; 
ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository 
mysql> reset slave; 
Query OK, 0 rows affected (0.00 sec) 
查看新从库/var/lib/mysql/master.info内容,从新change master 
mysql> change master to 
-> master_host='192.168.10.226', 
-> master_port=3306, 
-> master_user='repli', 
-> master_password='repli', 
-> master_log_file='mysql-bin.000001', 
-> master_log_pos=120; 
Query OK, 0 rows affected, 2 warnings (0.05 sec) 
mysql> start slave; 
Query OK, 0 rows affected (0.05 sec) 

6、重启新主从库,开启slave进程,检查是否正常 
[root@localhost mysql]# service mysql restart 
Shutting down MySQL... [ OK ] 
Starting MySQL.... [ OK ] 
新主库 
mysql> show processlist; 
+----+-------+----------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+ 
| Id | User | Host | db | Command | Time | State | Info | 
+----+-------+----------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+ 
| 2 | root | localhost | NULL | Query | 0 | init | show processlist | 
| 4 | repli | 192.168.10.197:56038 | NULL | Binlog Dump | 283 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL | 
+----+-------+----------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+ 
2 rows in set (0.00 sec) 
mysql> show master status\G 
*************************** 1. row *************************** 
File: mysql-bin.000002 
Position: 120 
Binlog_Do_DB: 
Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec) 
新从库 
start slave; 
mysql> show processlist; 
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+ 
| Id | User | Host | db | Command | Time | State | Info | 
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+ 
| 1 | root | localhost | NULL | Query | 0 | init | show processlist | 
| 2 | system user | | NULL | Connect | 67 | Waiting for master to send event | NULL | 
| 3 | system user | | NULL | Connect | 67 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL | 
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+ 
3 rows in set (0.00 sec) 
mysql> show slave status\G 
*************************** 1. row *************************** 
Slave_IO_State: Waiting for master to send event 
Master_Host: 192.168.10.226 
Master_User: repli 
Master_Port: 3306 
Connect_Retry: 60 
Master_Log_File: mysql-bin.000002 
Read_Master_Log_Pos: 120 
Relay_Log_File: mysql-relay-bin.000005 
Relay_Log_Pos: 283 
Relay_Master_Log_File: mysql-bin.000002 
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: 120 
Relay_Log_Space: 456 
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: 60 
Master_UUID: c954f3a2-21d0-11e4-a4b2-000c2981e58a 
Master_Info_File: /var/lib/mysql/master.info 
SQL_Delay: 0 
SQL_Remaining_Delay: NULL 
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it 
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 
1 row in set (0.00 sec) 
新主从同步,切换完成! 

[root@localhost mysql]# cat master.info     --文件记录的信息 
23 
mysql-bin.000002 
120 
192.168.10.226 
repli 
repli 
3306 
60 
.......
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
Kubernetes Cloud Native 关系型数据库
提升数据安全与性能,掌握Helm一键部署MySQL 8.0主从技巧
【4月更文挑战第9天】提升数据安全与性能,掌握Helm一键部署MySQL 8.0主从技巧
53 0
|
2月前
|
关系型数据库 MySQL 开发工具
MySQL5.7主从配置(Docker)
MySQL5.7主从配置(Docker)
741 0
|
2月前
|
SQL 关系型数据库 MySQL
解决MySQL主从慢同步问题的常见的解决方案:
解决MySQL主从慢同步问题的方法有很多,以下是一些常见的解决方案: 1. 检查网络连接:确保主从服务器之间的网络连接稳定,避免网络延迟或丢包导致数据同步缓慢。 2. 优化数据库配置:调整MySQL的配置参数,如增大binlog文件大小、调整innodb_flush_log_at_trx_commit等参数,以提高主从同步性能。 3. 检查IO线程和SQL线程状态:通过SHOW SLAVE STATUS命令检查IO线程和SQL线程的状态,确保它们正常运行并没有出现错误。 4. 检查主从日志位置:确认主从服务器的binlog文件和位置是否正确,避免由于错误的日志位置导致同步延迟。 5.
173 1
|
5月前
|
负载均衡 关系型数据库 MySQL
MySQL主从架构的搭建
MySQL主从架构的搭建
61 3
|
5月前
|
SQL 关系型数据库 MySQL
小白带你学习linux的mysql服务(主从mysql服务和读写分离三十一)
小白带你学习linux的mysql服务(主从mysql服务和读写分离三十一)
77 0
|
4月前
|
SQL 关系型数据库 MySQL
MySQL-主从架构的搭建
MySQL-主从架构的搭建
66 0
|
3天前
|
关系型数据库 MySQL Linux
服务器Linux系统配置mysql数据库主从自动备份
这是一个基本的配置主从复制和设置自动备份的指南。具体的配置细节和命令可能因您的环境和需求而有所不同,因此建议在操作前详细阅读MySQL文档和相关资源,并谨慎操作以避免数据丢失或不一致。
11 3
|
4天前
|
关系型数据库 MySQL Linux
本地虚拟机centos7通过docker安装主从mysql5.7.21
本地虚拟机centos7通过docker安装主从mysql5.7.21
12 0
|
15天前
|
SQL 关系型数据库 MySQL
MySQL主从模式最佳实践
主从模式是很常见的数据库存储解决方案,一主多从,当然还有双主模式(多主模式),你对数据库的主从模式有哪些见解,欢迎跟 V 哥聊聊。
|
1月前
|
SQL 关系型数据库 MySQL
mysql主从同步出错解决办法
mysql主从同步出错解决办法
17 0