1.防火墙

firewall-cmd --add-port=/tcp --permanent
firewall-cmd --reload

2.SELINUX

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce

3.软件包需求

yum install -y perl-Class-Load.noarch perl-DBD-MySQL

#以下需下载,地址: https://pkgs.org/
yum install -y perl-Log-Dispatch-2.41-.of.el7.noarch.rpm
yum install -y perl-Config-Tiny-2.14-.el7.noarch.rpm
yum install -y perl-Parallel-ForkManager-1.06-.of.el7.noarch.rpm #MHA包需下载,地址 https://github.com/yoshinorim
mha4mysql-manager-0.58-.el7.centos.noarch.rpm #建议安装在独立服务器,也可选择任一从库安装
mha4mysql-node-0.58-.el7.centos.noarch.rpm #安装在所有节点

4.密码认证插件需要用 mysql_native_password

[mysqld]
default_authentication_plugin=mysql_native_password
alter user rep identified with 'mysql_native_password' by 'rep';
alter user root identified with 'mysql_native_password' by 'root ';

5. 检查配置是否成功

建议所有从库都置为只读模式 read_only=1,但这实际并不影响MHA搭建。

[root@mysql3 soft]# masterha_check_repl --conf=/etc/masterha_default.cnf
Mon Jun :: - [info] Reading default configuration from /etc/masterha_default.cnf..
Mon Jun :: - [info] Reading application default configuration from /etc/masterha_default.cnf..
Mon Jun :: - [info] Reading server configuration from /etc/masterha_default.cnf..
Mon Jun :: - [info] MHA::MasterMonitor version 0.58.
Mon Jun :: - [info] GTID failover mode =
Mon Jun :: - [info] Dead Servers:
Mon Jun :: - [info] Alive Servers:
Mon Jun :: - [info] mysql1(192.168.20.81:)
Mon Jun :: - [info] mysql2(192.168.20.82:)
Mon Jun :: - [info] mysql3(192.168.20.83:)
Mon Jun :: - [info] mysql4(192.168.20.84:)
Mon Jun :: - [info] Alive Slaves:
Mon Jun :: - [info] mysql2(192.168.20.82:) Version=8.0. (oldest major version between slaves) log-bin:enabled
Mon Jun :: - [info] GTID ON
Mon Jun :: - [info] Replicating from 192.168.20.81(192.168.20.81:)
Mon Jun :: - [info] Primary candidate for the new Master (candidate_master is set)
Mon Jun :: - [info] mysql3(192.168.20.83:) Version=8.0. (oldest major version between slaves) log-bin:enabled
Mon Jun :: - [info] GTID ON
Mon Jun :: - [info] Replicating from 192.168.20.81(192.168.20.81:)
Mon Jun :: - [info] Not candidate for the new Master (no_master is set)
Mon Jun :: - [info] mysql4(192.168.20.84:) Version=8.0. (oldest major version between slaves) log-bin:enabled
Mon Jun :: - [info] GTID ON
Mon Jun :: - [info] Replicating from 192.168.20.81(192.168.20.81:)
Mon Jun :: - [info] Not candidate for the new Master (no_master is set)
Mon Jun :: - [info] Current Alive Master: mysql1(192.168.20.81:)
Mon Jun :: - [info] Checking slave configurations..
Mon Jun :: - [info] read_only= is not set on slave mysql2(192.168.20.82:).
Mon Jun :: - [info] Checking replication filtering settings..
Mon Jun :: - [info] binlog_do_db= , binlog_ignore_db=
Mon Jun :: - [info] Replication filtering check ok.
Mon Jun :: - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Mon Jun :: - [info] Checking SSH publickey authentication settings on the current master..
Mon Jun :: - [info] HealthCheck: SSH to mysql1 is reachable.
Mon Jun :: - [info]
mysql1(192.168.20.81:) (current master)
+--mysql2(192.168.20.82:)
+--mysql3(192.168.20.83:)
+--mysql4(192.168.20.84:) Mon Jun :: - [info] Checking replication health on mysql2..
Mon Jun :: - [info] ok.
Mon Jun :: - [info] Checking replication health on mysql3..
Mon Jun :: - [info] ok.
Mon Jun :: - [info] Checking replication health on mysql4..
Mon Jun :: - [info] ok.
Mon Jun :: - [warning] master_ip_failover_script is not defined.
Mon Jun :: - [warning] shutdown_script is not defined.
Mon Jun :: - [info] Got exit code (Not master dead). MySQL Replication Health is OK.

6.以上我用的GTID 复制模式

[mysqld]
enforce_gtid_consistency=ON
gtid_mode=on
--从库执行如下命令,启动复制
change master to
master_host='192.168.20.81',
master_port=3306,
master_auto_position=1;
start slave user='rep' password='rep';

7.MHA 相关命令

masterha_check_ssh --conf=/etc/masterha_default.cnf
masterha_check_repl --conf=/etc/masterha_default.cnf masterha_master_switch        #可用来执行 fail over/online master switch
# For master failover
masterha_master_switch --master_state=dead
--global_conf=/etc/masterha_default.cnf
--conf=/usr/local/masterha/conf/app1.cnf --dead_master_host=host1
# For online master switch
masterha_master_switch --master_state=alive
--global_conf=/etc/masterha_default.cnf
--conf=/usr/local/masterha/conf/app1.cnf masterha_master_monitor --global_conf=/etc/masterha_default.cnf --conf=/usr/local/masterha/app1.cnf masterha_secondary_check -s secondary_host1 -s secondary_host2 .. --user=ssh_username --master_host=host --master_ip=ip --master_port=port masterha_manager --global_conf=/etc/masterha_default.cnf --conf=/usr/local/masterha/conf/app1.cnf # 以上的--global_conf=/etc/masterha_default.cnf 参数均可省略

8.MHA 在线切换测试

[root@mysql3 soft]# masterha_master_switch --master_state=alive --conf=/etc/masterha_default.cnf
Mon Jun :: - [info] MHA::MasterRotate version 0.58.
Mon Jun :: - [info] Starting online master switch..
Mon Jun :: - [info]
Mon Jun :: - [info] * Phase : Configuration Check Phase..
Mon Jun :: - [info]
Mon Jun :: - [info] Reading default configuration from /etc/masterha_default.cnf..
Mon Jun :: - [info] Reading application default configuration from /etc/masterha_default.cnf..
Mon Jun :: - [info] Reading server configuration from /etc/masterha_default.cnf..
Mon Jun :: - [info] GTID failover mode =
Mon Jun :: - [info] Current Alive Master: mysql1(192.168.20.81:)
Mon Jun :: - [info] Alive Slaves:
Mon Jun :: - [info] mysql2(192.168.20.82:) Version=8.0. (oldest major version between slaves) log-bin:enabled
Mon Jun :: - [info] GTID ON
Mon Jun :: - [info] Replicating from 192.168.20.81(192.168.20.81:)
Mon Jun :: - [info] Primary candidate for the new Master (candidate_master is set)
Mon Jun :: - [info] mysql3(192.168.20.83:) Version=8.0. (oldest major version between slaves) log-bin:enabled
Mon Jun :: - [info] GTID ON
Mon Jun :: - [info] Replicating from 192.168.20.81(192.168.20.81:)
Mon Jun :: - [info] Not candidate for the new Master (no_master is set)
Mon Jun :: - [info] mysql4(192.168.20.84:) Version=8.0. (oldest major version between slaves) log-bin:enabled
Mon Jun :: - [info] GTID ON
Mon Jun :: - [info] Replicating from 192.168.20.81(192.168.20.81:)
Mon Jun :: - [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 mysql1(192.168.20.81:)? (YES/no): YES
Mon Jun :: - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..
Mon Jun :: - [info] ok.
Mon Jun :: - [info] Checking MHA is not monitoring or doing failover..
Mon Jun :: - [info] Checking replication health on mysql2..
Mon Jun :: - [info] ok.
Mon Jun :: - [info] Checking replication health on mysql3..
Mon Jun :: - [info] ok.
Mon Jun :: - [info] Checking replication health on mysql4..
Mon Jun :: - [info] ok.
Mon Jun :: - [info] Searching new master from slaves..
Mon Jun :: - [info] Candidate masters from the configuration file:
Mon Jun :: - [info] mysql1(192.168.20.81:) Version=8.0. log-bin:enabled
Mon Jun :: - [info] GTID ON
Mon Jun :: - [info] mysql2(192.168.20.82:) Version=8.0. (oldest major version between slaves) log-bin:enabled
Mon Jun :: - [info] GTID ON
Mon Jun :: - [info] Replicating from 192.168.20.81(192.168.20.81:)
Mon Jun :: - [info] Primary candidate for the new Master (candidate_master is set)
Mon Jun :: - [info] Non-candidate masters:
Mon Jun :: - [info] mysql3(192.168.20.83:) Version=8.0. (oldest major version between slaves) log-bin:enabled
Mon Jun :: - [info] GTID ON
Mon Jun :: - [info] Replicating from 192.168.20.81(192.168.20.81:)
Mon Jun :: - [info] Not candidate for the new Master (no_master is set)
Mon Jun :: - [info] mysql4(192.168.20.84:) Version=8.0. (oldest major version between slaves) log-bin:enabled
Mon Jun :: - [info] GTID ON
Mon Jun :: - [info] Replicating from 192.168.20.81(192.168.20.81:)
Mon Jun :: - [info] Not candidate for the new Master (no_master is set)
Mon Jun :: - [info] Searching from candidate_master slaves which have received the latest relay log events..
Mon Jun :: - [info]
From:
mysql1(192.168.20.81:) (current master)
+--mysql2(192.168.20.82:)
+--mysql3(192.168.20.83:)
+--mysql4(192.168.20.84:) To:
mysql2(192.168.20.82:) (new master)
+--mysql3(192.168.20.83:)
+--mysql4(192.168.20.84:) Starting master switch from mysql1(192.168.20.81:) to mysql2(192.168.20.82:)? (yes/NO): yes
Mon Jun :: - [info] Checking whether mysql2(192.168.20.82:) is ok for the new master..
Mon Jun :: - [info] ok.
Mon Jun :: - [info] ** Phase : Configuration Check Phase completed.
Mon Jun :: - [info]
Mon Jun :: - [info] * Phase : Rejecting updates Phase..
Mon Jun :: - [info]
FLUSH NO_WRITE_TO_BINLOG TABLES is not defined. If you do not disable writes on the current master manually, applications keep writing on the current master. Is it ok to proceed? (yes/NO): yes
Mon Jun :: - [info] Locking all tables on the orig master to reject updates from everybody (including root):
Mon Jun :: - [info] Executing FLUSH TABLES WITH READ LOCK..
Mon Jun :: - [info] ok.
Mon Jun :: - [info] Orig master binlog:pos is binlog.:.
Mon Jun :: - [info] Waiting to execute all relay logs on mysql2(192.168.20.82:)..
Mon Jun :: - [info] master_pos_wait(binlog.:) completed on mysql2(192.168.20.82:). Executed events.
Mon Jun :: - [info] done.
Mon Jun :: - [info] Getting new master's binlog name and position..
Mon Jun :: - [info] binlog.:
Mon Jun :: - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='mysql2 or 192.168.20.82', MASTER_PORT=, MASTER_AUTO_POSITION=, MASTER_USER='rep', MASTER_PASSWORD='xxx';
Mon Jun :: - [info] Setting read_only= on mysql2(192.168.20.82:)..
Mon Jun :: - [info] ok.
Mon Jun :: - [info]
Mon Jun :: - [info] * Switching slaves in parallel..
Mon Jun :: - [info]
Mon Jun :: - [info] -- Slave switch on host mysql3(192.168.20.83:) started, pid:
Mon Jun :: - [info]
Mon Jun :: - [info] -- Slave switch on host mysql4(192.168.20.84:) started, pid:
Mon Jun :: - [info]
Mon Jun :: - [info] Log messages from mysql3 ...
Mon Jun :: - [info]
Mon Jun :: - [info] Waiting to execute all relay logs on mysql3(192.168.20.83:)..
Mon Jun :: - [info] master_pos_wait(binlog.:) completed on mysql3(192.168.20.83:). Executed events.
Mon Jun :: - [info] done.
Mon Jun :: - [info] Resetting slave mysql3(192.168.20.83:) and starting replication from the new master mysql2(192.168.20.82:)..
Mon Jun :: - [info] Executed CHANGE MASTER.
Mon Jun :: - [info] Slave started.
Mon Jun :: - [info] End of log messages from mysql3 ...
Mon Jun :: - [info]
Mon Jun :: - [info] -- Slave switch on host mysql3(192.168.20.83:) succeeded.
Mon Jun :: - [info] Log messages from mysql4 ...
Mon Jun :: - [info]
Mon Jun :: - [info] Waiting to execute all relay logs on mysql4(192.168.20.84:)..
Mon Jun :: - [info] master_pos_wait(binlog.:) completed on mysql4(192.168.20.84:). Executed events.
Mon Jun :: - [info] done.
Mon Jun :: - [info] Resetting slave mysql4(192.168.20.84:) and starting replication from the new master mysql2(192.168.20.82:)..
Mon Jun :: - [info] Executed CHANGE MASTER.
Mon Jun :: - [info] Slave started.
Mon Jun :: - [info] End of log messages from mysql4 ...
Mon Jun :: - [info]
Mon Jun :: - [info] -- Slave switch on host mysql4(192.168.20.84:) succeeded.
Mon Jun :: - [info] Unlocking all tables on the orig master:
Mon Jun :: - [info] Executing UNLOCK TABLES..
Mon Jun :: - [info] ok.
Mon Jun :: - [info] All new slave servers switched successfully.
Mon Jun :: - [info]
Mon Jun :: - [info] * Phase : New master cleanup phase..
Mon Jun :: - [info]
Mon Jun :: - [info] mysql2: Resetting slave info succeeded.
Mon Jun :: - [info] Switching master to mysql2(192.168.20.82:) completed successfully.

执行过程中提供了几次YES输入,还有几个重要的语句:

检查阶段:
master 执行 FLUSH NO_WRITE_TO_BINLOG TABLES
搜寻candidate_master= 的slave 节点,选择其为新master 执行阶段:
FLUSH NO_WRITE_TO_BINLOG TABLES is not defined. If you do not disable writes on the current master manually, applications keep writing on the current master. Is it ok to proceed? (yes/NO): yes
Locking all tables on the orig master to reject updates from everybody (including root):
Executing FLUSH TABLES WITH READ LOCK..
All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='mysql2 or 192.168.20.82', MASTER_PORT=, MASTER_AUTO_POSITION=, MASTER_USER='rep', MASTER_PASSWORD='xxx';
Setting read_only= on mysql2(192.168.20.82:)..
Unlocking all tables on the orig master:
Executing UNLOCK TABLES..

9.在线切换注意点

1. 会从 candidate_master=1的slave节点中选择新master
2.被选择的新master 修改为read_only=0
3.原来的master 修改为read_only=1
4. 切换后,原master 不会转为新master 的 slave。
5.配置文件不会发生改变,所以重启需注意。
 

10.将原master 改为新master 的slave

--将原master 修改为新master 的从库
change master to master_host='192.168.20.82',master_port=3306,master_auto_position=1;
start slave user='rep' password='rep';
show slave status \G

11. 切换回原来的master

# masterha_master_switch --master_state=alive --conf=/etc/masterha_default.cnf 

同样的需要把node2 改为slave
change master to master_host='192.168.20.81',master_port=3306,master_auto_position=1;
start slave user='rep' password='rep';
show slave status \G

  

【Mysql MHA】CentOS7.6+Mysql8.0.16 入坑的更多相关文章

  1. 【Linux】【MySQL】CentOS7、MySQL8.0.13 骚操作速查笔记——专治各种忘词水土不服

    1.前言 [Linux][MySQL]CentOS7安装最新版MySQL8.0.13(最新版MySQL从安装到运行) 专治各种忘词,各种水土不服. - -,就是一个健忘贵的速查表:(当然不包括SQL的 ...

  2. Linux(CentOS7)下rpm安装MySQL8.0.16

    记录一下自己在 CentOS7 下 rpm 安装 MySQL8.0.16 的过程. 一.准备工作 1. 下载MySQL所需要的安装包 从 MySQL官网 下载,上传至 CentOS 系统 /usr/l ...

  3. centos7编译安装LNMP(nginx-1.16.0,mysql8.0.16,php-7.3.6)常见问题报错及解决方法

    LNMP的安装与配置 nginx-1.16.0安装及配置: 第一步:前往官网下载nignx源码包 下载完毕后上传至服务器(先安装lrzsz) yum -y install lrzsz 安装完毕后执行: ...

  4. Centos7安装MySQL8.0

    请到这个地址看:https://www.cnblogs.com/kevingrace/p/10482469.html Centos7安装MySQL8.0 - 操作手册 一.yum安装方式: 卸载之前版 ...

  5. CentOS7安装MySQL8.0小计

    之前讲配置文件和权限的时候有很多MySQL8的知识,有同志说安装不太一样,希望发个文,我这边简单演示一下 1.环境安装 下载MySQL提供的CentOS7的yum源 官方文档:<https:// ...

  6. mysql8.0.16操作记录

    mysql8.0.16操作记录 2.1.登录 -uroot -p'AnvcTMagdLarwNV3CKaC' mysql: [Warning] Using a password on the comm ...

  7. 【SQL必知必会笔记(1)】数据库基础、SQL、MySQL8.0.16下数据库、表的创建及数据插入

    文章目录 1.数据库基础 1.1 数据库(database) 1.2 表(table) 1.3 列和数据类型 1.4 行 1.5 主键 2.什么是SQL 3.创建后续练习所需数据库.表(MySQL8. ...

  8. Centos7安装MySQL8.0 - 操作手册

    MySQL 8 正式版 8.0.11 已发布,官方表示 MySQL 8 要比 MySQL 5.7 快 2 倍,还带来了大量的改进和更快的性能! 一.  Mysql8.0版本相比之前版本的一些特性 1) ...

  9. MySQL入门介绍(mysql-8.0.13)

    MySQL入门介绍(mysql-8.0.13单机部署) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL数据库介绍 1>.MySQL是一种开放源代码的关系型数据库 ...

随机推荐

  1. 微信支付 composer 方法 --- 实测有效

    <h1 align="center">Pay</h1> <p align="center"> <a href=&quo ...

  2. Java不写文件,LOAD DATA LOCAL INFILE大批量导入数据到MySQL的实现(转)

    MySQL使用load data local infile 从文件中导入数据比insert语句要快,MySQL文档上说要快20倍左右.但是这个方法有个缺点,就是导入数据之前,必须要有文件,也就是说从文 ...

  3. iOS笔试题02

    1. Difference between shallow copy and deep copy? 1> 浅拷贝:指针(地址)拷贝,不会产生新对象 2> 深拷贝:内容拷贝,会产生新对象 2 ...

  4. Tesnsorflow命名空间与变量管理参数reuse

    一.TensorFlow中变量管理reuse参数的使用 1.TensorFlow用于变量管理的函数主要有两个:  (1)tf.get_variable:用于创建或获取变量的值  (2)tf.varia ...

  5. Egret入门学习日记 --- 第十一篇(书中 4.1~4.6节 内容)

    第十一篇(书中 4.1~4.6节 内容) 好了,到了这篇开始,前三章都记录完了. 接下来就是到第四章了. 4.1节 的内容总结一下重点: 1.resource目录下default.res.json文件 ...

  6. Python之Numpy库常用函数大全(含注释)(转)

    为收藏学习,特转载:https://blog.csdn.net/u011995719/article/details/71080987 前言:最近学习Python,才发现原来python里的各种库才是 ...

  7. Python: 对于DataFrame.loc传入列表和传入元组输出区别的理解

    def test(): import pandas as pd tuples = [ ('cobra', 'mark i'), ('cobra', 'mark ii'), ('sidewinder', ...

  8. Asp.Net Core 客户端验证和远程验证

    我们先来看这样一个注册页面和它的后台Model @model RegisterViewModel @{ ViewBag.Title = "用户注册"; } <h1>用户 ...

  9. JavaSE基础(二)--Java环境配置

    Java 开发环境配置 在本章节中我们将为大家介绍如何搭建Java开发环境. Windows 上安装开发环境 Linux 上安装开发环境 安装 Eclipse 运行 Java window系统安装ja ...

  10. navicat-use

    https://blog.csdn.net/weixin_44496987/article/details/87186071