我的编程空间,编程开发者的网络收藏夹
学习永远不晚

Mysql MHA部署中如何手动切换

短信预约 -IT技能 免费直播动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

Mysql MHA部署中如何手动切换

这篇文章给大家介绍Mysql MHA部署中如何手动切换,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

Mysql MHA部署-手动切换


架构说明:

Mysql MHA部署中如何手动切换

参考:http://www.zhaibibei.cn/mysql/mha/

1 检查从库现有状态(188,223)

---show slave status\G

2 查看管理节点日志(222)

通过如下命令事实查看MHA当前状态

tail -f /etc/mha/manager/mha.log

3 关闭MHA的管理进程(222)

[root@rac4 ~]# masterha_stop -conf=/etc/mha/mha.conf 

MHA Manager is not running on mha(2:NOT_RUNNING).

4.手动切换

相关命令如下:

masterha_master_switch   -master_state=alive –orig_master_is_new_slave –conf=/etc/mha/mha.conf

-master_state=alive 代表告诉MHA原master还是存活的,不需要将其从配置文件删除

–orig_master_is_new_slave 参数代表原master会自动同步新的master

--还有一些其他的参数如下

-running_updates_limit 如果主库的写操作时间超过了该参数,则退出切换

–interactive=0 代表直接确认,不需要输入YES

切换过程如下:

[root@rac4 ~]# masterha_master_switch -master_state=alive -orig_master_is_new_slave -conf=/etc/mha/mha.conf

Sat Mar 14 22:17:37 2020 - [info] MHA::MasterRotate version 0.56.

Sat Mar 14 22:17:37 2020 - [info] Starting online master switch..

Sat Mar 14 22:17:37 2020 - [info] 

Sat Mar 14 22:17:37 2020 - [info] * Phase 1: Configuration Check Phase..

Sat Mar 14 22:17:37 2020 - [info] 

Sat Mar 14 22:17:37 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.

Sat Mar 14 22:17:37 2020 - [info] Reading application default configuration from /etc/mha/mha.conf..

Sat Mar 14 22:17:37 2020 - [info] Reading server configuration from /etc/mha/mha.conf..

Sat Mar 14 22:17:38 2020 - [info] GTID failover mode = 1

Sat Mar 14 22:17:38 2020 - [info] Current Alive Master: rac1(192.168.2.187:3306)

Sat Mar 14 22:17:38 2020 - [info] Alive Slaves:

Sat Mar 14 22:17:38 2020 - [info]   rac2(192.168.2.188:3306)  Version=5.7.28-log (oldest major version between slaves) log-bin:enabled

Sat Mar 14 22:17:38 2020 - [info]     GTID ON

Sat Mar 14 22:17:38 2020 - [info]     Replicating from 192.168.2.187(192.168.2.187:3306)

Sat Mar 14 22:17:38 2020 - [info]     Primary candidate for the new Master (candidate_master is set)

Sat Mar 14 22:17:38 2020 - [info]   rac3(192.168.2.223:3306)  Version=5.7.28-log (oldest major version between slaves) log-bin:enabled

Sat Mar 14 22:17:38 2020 - [info]     GTID ON

Sat Mar 14 22:17:38 2020 - [info]     Replicating from 192.168.2.187(192.168.2.187:3306)

Sat Mar 14 22:17:38 2020 - [info]     Not candidate for the new Master (no_master is set)

It is better to execute FLUSH NO_WRITE_TO_BINLOG TABLES on the master before switching. Is it ok to execute on rac1(192.168.2.187:3306)? (YES/no): YES

Sat Mar 14 22:19:01 2020 - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..

Sat Mar 14 22:19:01 2020 - [info]  ok.

Sat Mar 14 22:19:01 2020 - [info] Checking MHA is not monitoring or doing failover..

Sat Mar 14 22:19:01 2020 - [info] Checking replication health on rac2..

Sat Mar 14 22:19:01 2020 - [info]  ok.

Sat Mar 14 22:19:01 2020 - [info] Checking replication health on rac3..

Sat Mar 14 22:19:01 2020 - [info]  ok.

Sat Mar 14 22:19:01 2020 - [info] Searching new master from slaves..

Sat Mar 14 22:19:01 2020 - [info]  Candidate masters from the configuration file:

Sat Mar 14 22:19:01 2020 - [info]   rac1(192.168.2.187:3306)  Version=5.7.28-log log-bin:enabled

Sat Mar 14 22:19:01 2020 - [info]     GTID ON

Sat Mar 14 22:19:01 2020 - [info]   rac2(192.168.2.188:3306)  Version=5.7.28-log (oldest major version between slaves) log-bin:enabled

Sat Mar 14 22:19:01 2020 - [info]     GTID ON

Sat Mar 14 22:19:01 2020 - [info]     Replicating from 192.168.2.187(192.168.2.187:3306)

Sat Mar 14 22:19:01 2020 - [info]     Primary candidate for the new Master (candidate_master is set)

Sat Mar 14 22:19:01 2020 - [info]  Non-candidate masters:

Sat Mar 14 22:19:01 2020 - [info]   rac3(192.168.2.223:3306)  Version=5.7.28-log (oldest major version between slaves) log-bin:enabled

Sat Mar 14 22:19:01 2020 - [info]     GTID ON

Sat Mar 14 22:19:01 2020 - [info]     Replicating from 192.168.2.187(192.168.2.187:3306)

Sat Mar 14 22:19:01 2020 - [info]     Not candidate for the new Master (no_master is set)

Sat Mar 14 22:19:01 2020 - [info]  Searching from candidate_master slaves which have received the latest relay log events..

Sat Mar 14 22:19:01 2020 - [info] 

From:

rac1(192.168.2.187:3306) (current master)

 +--rac2(192.168.2.188:3306)

 +--rac3(192.168.2.223:3306)

To:

rac2(192.168.2.188:3306) (new master)

 +--rac3(192.168.2.223:3306)

 +--rac1(192.168.2.187:3306)

Starting master switch from rac1(192.168.2.187:3306) to rac2(192.168.2.188:3306)? (yes/NO): yes

Sat Mar 14 22:19:32 2020 - [info] Checking whether rac2(192.168.2.188:3306) is ok for the new master..

Sat Mar 14 22:19:32 2020 - [info]  ok.

Sat Mar 14 22:19:32 2020 - [info] rac1(192.168.2.187:3306): SHOW SLAVE STATUS returned empty result. To check replication filtering rules, temporarily executing CHANGE MASTER to a dummy host.

Sat Mar 14 22:19:32 2020 - [info] rac1(192.168.2.187:3306): Resetting slave pointing to the dummy host.

Sat Mar 14 22:19:32 2020 - [info] ** Phase 1: Configuration Check Phase completed.

Sat Mar 14 22:19:32 2020 - [info] 

Sat Mar 14 22:19:32 2020 - [info] * Phase 2: Rejecting updates Phase..

Sat Mar 14 22:19:32 2020 - [info] 

Sat Mar 14 22:19:32 2020 - [info] Executing master ip online change script to disable write on the current master:

Sat Mar 14 22:19:32 2020 - [info]   /etc/mha/script/master_ip_online_change --command=stop --orig_master_host=rac1 --orig_master_ip=192.168.2.187 --orig_master_port=3306 --orig_master_user='monitor' --orig_master_password='123456' --new_master_host=rac2 --new_master_ip=192.168.2.188 --new_master_port=3306 --new_master_user='monitor' --new_master_password='123456' --orig_master_ssh_user=root --new_master_ssh_user=root   --orig_master_is_new_slave

Sat Mar 14 22:19:32 2020 756531 Set read_only on the new master.. ok.

Sat Mar 14 22:19:32 2020 761639 Set read_only=1 on the orig master.. ok.

Sat Mar 14 22:19:32 2020 763740 Killing all application threads..

Sat Mar 14 22:19:32 2020 763785 done.

Disabling the VIP an old master: rac1 

eth0:2: ERROR while getting interface flags: No such device

Sat Mar 14 22:19:33 2020 - [info]  ok.

Sat Mar 14 22:19:33 2020 - [info] Locking all tables on the orig master to reject updates from everybody (including root):

Sat Mar 14 22:19:33 2020 - [info] Executing FLUSH TABLES WITH READ LOCK..

Sat Mar 14 22:19:33 2020 - [info]  ok.

Sat Mar 14 22:19:33 2020 - [info] Orig master binlog:pos is mysql-bin.000009:194.

Sat Mar 14 22:19:33 2020 - [info]  Waiting to execute all relay logs on rac2(192.168.2.188:3306)..

Sat Mar 14 22:19:33 2020 - [info]  master_pos_wait(mysql-bin.000009:194) completed on rac2(192.168.2.188:3306). Executed 0 events.

Sat Mar 14 22:19:33 2020 - [info]   done.

Sat Mar 14 22:19:33 2020 - [info] Getting new master's binlog name and position..

Sat Mar 14 22:19:33 2020 - [info]  mysql-bin.000006:3938

Sat Mar 14 22:19:33 2020 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='rac2 or 192.168.2.188', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='xxx';

Sat Mar 14 22:19:33 2020 - [info] Executing master ip online change script to allow write on the new master:

Sat Mar 14 22:19:33 2020 - [info]   /etc/mha/script/master_ip_online_change --command=start --orig_master_host=rac1 --orig_master_ip=192.168.2.187 --orig_master_port=3306 --orig_master_user='monitor' --orig_master_password='123456' --new_master_host=rac2 --new_master_ip=192.168.2.188 --new_master_port=3306 --new_master_user='monitor' --new_master_password='123456' --orig_master_ssh_user=root --new_master_ssh_user=root   --orig_master_is_new_slave

Sat Mar 14 22:19:33 2020 227880 Set read_only=0 on the new master.

Enabling the VIP 192.168.2.189 on the new master: rac2 

SIOCSIFADDR: No such device

eth0:2: ERROR while getting interface flags: No such device

SIOCSIFNETMASK: No such device

arping: Device eth0 not available.

Sat Mar 14 22:19:33 2020 - [info]  ok.

Sat Mar 14 22:19:33 2020 - [info] 

Sat Mar 14 22:19:33 2020 - [info] * Switching slaves in parallel..

Sat Mar 14 22:19:33 2020 - [info] 

Sat Mar 14 22:19:33 2020 - [info] -- Slave switch on host rac3(192.168.2.223:3306) started, pid: 12424

Sat Mar 14 22:19:33 2020 - [info] 

Sat Mar 14 22:19:34 2020 - [info] Log messages from rac3 ...

Sat Mar 14 22:19:34 2020 - [info] 

Sat Mar 14 22:19:33 2020 - [info]  Waiting to execute all relay logs on rac3(192.168.2.223:3306)..

Sat Mar 14 22:19:33 2020 - [info]  master_pos_wait(mysql-bin.000009:194) completed on rac3(192.168.2.223:3306). Executed 0 events.

Sat Mar 14 22:19:33 2020 - [info]   done.

Sat Mar 14 22:19:33 2020 - [info]  Resetting slave rac3(192.168.2.223:3306) and starting replication from the new master rac2(192.168.2.188:3306)..

Sat Mar 14 22:19:33 2020 - [info]  Executed CHANGE MASTER.

Sat Mar 14 22:19:33 2020 - [info]  Slave started.

Sat Mar 14 22:19:34 2020 - [info] End of log messages from rac3 ...

Sat Mar 14 22:19:34 2020 - [info] 

Sat Mar 14 22:19:34 2020 - [info] -- Slave switch on host rac3(192.168.2.223:3306) succeeded.

Sat Mar 14 22:19:34 2020 - [info] Unlocking all tables on the orig master:

Sat Mar 14 22:19:34 2020 - [info] Executing UNLOCK TABLES..

Sat Mar 14 22:19:34 2020 - [info]  ok.

Sat Mar 14 22:19:34 2020 - [info] Starting orig master as a new slave..

Sat Mar 14 22:19:34 2020 - [info]  Resetting slave rac1(192.168.2.187:3306) and starting replication from the new master rac2(192.168.2.188:3306)..

Sat Mar 14 22:19:34 2020 - [info]  Executed CHANGE MASTER.

Sat Mar 14 22:19:34 2020 - [info]  Slave started.

Sat Mar 14 22:19:34 2020 - [info] All new slave servers switched successfully.

Sat Mar 14 22:19:34 2020 - [info] 

Sat Mar 14 22:19:34 2020 - [info] * Phase 5: New master cleanup phase..

Sat Mar 14 22:19:34 2020 - [info] 

Sat Mar 14 22:19:34 2020 - [info]  rac2: Resetting slave info succeeded.

Sat Mar 14 22:19:34 2020 - [info] Switching master to rac2(192.168.2.188:3306) completed successfully.

5.切换阶段

执行master_ip_online_change时当前master不可写

新的主库设置只读

旧的主库设置只读

禁用原主库VIP

旧主库设全局锁

获取新主库master信息

新主库设置VIP

新主库取消只读

异步从库重新同步至新主库

原主库释放全局锁

原主库同步至新主库

6.查看切换后状态

---223

mysql> show slave status \G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.2.188

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000006

          Read_Master_Log_Pos: 3938

               Relay_Log_File: mysql-relay.000005

                Relay_Log_Pos: 454

        Relay_Master_Log_File: mysql-bin.000006

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

----187

mysql> show slave status \G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: rac2

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000006

          Read_Master_Log_Pos: 3938

               Relay_Log_File: mysql-relay.000007

                Relay_Log_Pos: 454

        Relay_Master_Log_File: mysql-bin.000006

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: 

          Replicate_Ignore_DB: 

......

7.数据测试

---188:

mysql> use jumptest

mysql> insert into cjc01 values(3000);

Query OK, 1 row affected (0.02 sec)

mysql> select * from cjc01;

+------+

| id   |

+------+

|  100 |

|    3 |

| 3000 |

+------+

3 rows in set (0.00 sec)

---187:

mysql> select * from cjc01;

+------+

| id   |

+------+

|  100 |

|    3 |

| 3000 |

+------+

3 rows in set (0.01 sec)

---223:

mysql> select * from cjc01;

+------+

| id   |

+------+

|  100 |

|    3 |

| 3000 |

+------+

3 rows in set (0.01 sec)

关于Mysql MHA部署中如何手动切换就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

Mysql MHA部署中如何手动切换

下载Word文档到电脑,方便收藏和打印~

下载Word文档

猜你喜欢

MySQL之MHA高可用配置及故障切换实现详细部署步骤

一、MHA介绍 (一)、什么是MHA MHA(MasterHigh Availability)是一套优秀的MySQL高可用环境下故障切换和主从复制的软件。 MHA 的出现就是解决MySQL 单点的问题。 MySQL故障切换过程中,MHA能做
2022-05-24

mysql主备自动切换如何设置

要设置MySQL主备自动切换,可以使用MySQL Replication来实现主备复制和故障切换。以下是设置主备自动切换的步骤:配置主备复制:在主服务器上启用二进制日志,并在备服务器上启用复制功能。确保主服务器和备服务器之间可以互相通信,并
mysql主备自动切换如何设置
2024-04-09

docker中如何部署mysql集群

在Docker中部署MySQL集群可以使用Docker Compose来管理多个MySQL容器,并使用MySQL Router来实现负载均衡和故障转移。以下是一个简单的示例:创建一个名为docker-compose.yml的文件,并在其中定
docker中如何部署mysql集群
2024-04-09

如何实现H5+CSS3手指滑动切换图片

这篇文章将为大家详细讲解有关如何实现H5+CSS3手指滑动切换图片,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。包含3个文件:html、slider-H5.js、jquery.js。在html中可配置滑动
2023-06-09

手把手教你Windows如何在cmd中切换python版本

通常在Windows系统下我们可能安装了多个Python版本,那么该如何进行版本的切换呢?下面这篇文章主要给大家介绍了关于Windows如何在cmd中切换python版本的相关资料,需要的朋友可以参考下
2023-05-16

maven如何在tomcat8中实现自动部署

本文介绍了maven如何在tomcat8中实现自动部署,分享给大家,具体如下:1、首先需要有tomcat,并且配置好用户。配置的地方在tomcat的config文件中的tomcat-users.xml文件中2023-05-31

编程热搜

目录