`
LJ你是唯一LT
  • 浏览: 243367 次
社区版块
存档分类
最新评论

MySQL主从切换

阅读更多
环境:
原主库:192.168.10.197     ---新从库
原从库:192.168.10.226    ---新主库

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
.......
分享到:
评论

相关推荐

    线上经验谈MySQL主从切换的一般步骤

    MySQL主从切换是数据库高可用性架构中的常见操作,它涉及到数据复制、故障转移和负载均衡等关键概念。本文将详细解析这一过程,帮助你理解并掌握线上环境中MySQL主从切换的一般步骤。 首先,我们需要了解MySQL主从...

    使用keepalived实现对mysql主从复制的主备自动切换.docx

    "使用keepalived实现对mysql主从复制的主备自动切换" 本文主要讲解如何使用keepalived实现对mysql主从复制的主备自动切换。keepalived是一款高可用性解决方案,可以实现虚拟IP的管理和服务监控,在mysql主从复制...

    linux下mysql主从复制

    ### Linux 下 MySQL 主从复制详解 #### 一、概述 MySQL 的主从复制是一种非常重要的技术,它不仅可以提高系统的可用性和扩展性,还能为数据备份提供便利。在本篇文章中,我们将详细介绍如何在 Linux 操作系统环境...

    MHA实现mysql5.6主从切换安装配置教程.docx

    MySQL High Availability (MHA) 是一个用于管理 MySQL 主从复制的工具,旨在提高数据库系统的高可用性和容错性。在本教程中,我们将探讨如何在MySQL 5.6...理解这些概念和步骤是实现高效、可靠的MySQL主从切换的关键。

    MySQL 主从复制模式全面实践

    MySQL主从复制模式是数据库领域内一种重要的数据同步机制,它能够让一台主数据库服务器(master)的数据实时复制到一个或多个从数据库服务器(slave)上。这种机制在数据库的高可用性、数据备份、读写分离以及负载...

    mysql主从互备实战

    ### MySQL主从互备实战详解 #### 一、前言 在现代的互联网应用中,数据库作为关键的数据存储层,其稳定性和可用性至关重要。为了提高数据库系统的可靠性和响应速度,许多企业会采用MySQL主从互备(Master-Slave ...

    使用keepalived实现对mysql主从复制的主备自动切换.doc

    ### 使用Keepalived实现对MySQL主从复制的主备自动切换 #### 一、概述 在高可用(High Availability, HA)系统设计中,保证关键服务的不间断运行至关重要。MySQL作为广泛使用的开源关系型数据库管理系统,其高可用...

    window下mysql主从备份及切换

    根据提供的文件信息,本文将详细解释Windows环境下MySQL主从备份及主从切换的过程与关键配置。主要内容包括: 1. **主从备份的步骤详解** 2. **主从切换的步骤详解** 3. **多实例环境下的MySQL配置示例** ### 一、...

    MySQL主从复制搭建 需要用到的脚本

    MySQL主从复制是一种常见的数据库高可用性和数据冗余策略,它允许数据在多个服务器之间同步,确保即使在主服务器故障时,从服务器也能接管服务,从而实现无中断的服务连续性。这里提到的“MySQL主从复制搭建 需要...

    MySQL主从镜像双机

    MySQL主从镜像是数据库高可用性和数据冗余的一种常见实现方式,它允许数据在主服务器(Master)上被写入并同步到从服务器(Slave)。当主服务器出现故障时,可以快速将从服务器切换为新的主服务器,确保服务的连续性...

    mysql主从数据搭建问题处理

    本篇文章将详细探讨MySQL主从数据搭建过程中的问题处理,以及相关源码和工具的应用。 MySQL主从复制是指在一个MySQL集群中,主服务器(Master)处理所有写操作,而从服务器(Slave)则同步主服务器上的数据变更,...

    MySQL主从复制架构原理与配置实践

    ### MySQL主从复制架构原理与配置实践 #### 一、MySQL主从复制概述 MySQL主从复制是一种常用的技术手段,用于提高数据库系统的可用性、扩展性和数据安全性。它通过将一个MySQL实例(主服务器)的数据实时复制到一...

    mysql主从配置资源

    MySQL主从配置是数据库高可用性和负载均衡的一种常见策略,尤其在Windows环境下,为了实现读写分离、主从复制和一主多从架构,可以极大地提高数据库系统的性能和稳定性。结合Spring框架,我们可以轻松地在应用程序中...

    MySQL主从备份+Mycat读写分离学习笔记

    MySQL主从备份是一种高可用性解决方案,通过复制主服务器的数据到从服务器,实现数据的冗余和故障切换。主要包含以下几个步骤: 1. **配置复制环境**:在主服务器上设置binlog(二进制日志),记录所有改变数据库的...

    MYSQL主从复制高可用实施手册

    这个实施方案不支持自动的主从切换,因此在主节点出现问题时,需要人工介入完成数据同步、复制设置更新和流量切换等步骤。为了提高可用性,可以考虑使用更高级的高可用性解决方案,如MySQL Cluster、Galera Cluster...

Global site tag (gtag.js) - Google Analytics