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. Core Data 的使用

    一.概念 1.Core Data 是数据持久化存储的最佳方式 2.数据最终的存储类型可以是:SQLite数据库,XML,二进制,内存里,或自定义数据类型 在Mac OS X 10.5Leopard及以 ...

  2. ubuntu 18.04安装jdk8和eclipse

    JDK8的安装 1.安装ppa sudo add-apt-repository ppa:webupd8team/java sudo apt-get update 2.安装JDK sudo apt-ge ...

  3. 学习docker笔记1

    docker是一个能够把开发应用程序自动部署到容器的开源引擎 docker通过namespace实现了资源隔离,通过cgroups实现了资源限制,通过写时复制机制(copy-on-write)实现了高 ...

  4. 【ARM-Linux开发】【QT开发】Couldnt load module input, no modules loaded

    export TSLIB_FBDEVICE=/dev/fb0 export TSLIB_PLUGINDIR=/usr/lib/ts  (input.so 所在目录)

  5. win7/Ubuntu双系统给Ubuntu扩大硬盘

    问题 电脑上装了windows 7和ubuntu双系统,起初分配硬盘的时候,给ubuntu划分的量较少,如今硬盘空间已不够用,另外挂载分区也不够方便.希望能在不重装系统的情况下,从win7的硬盘中分一 ...

  6. golang 切片和map查询比较

    package main import ( "fmt" "time" ) var testTimeSlice = []string{"aa" ...

  7. Chrome 浏览器光标定位到地址栏

    Windows: Ctrl + L 或 Alt + D Mac: Command + L Linux: Ctrl + L

  8. [转帖]Linux Shell常用技巧(五)

    Linux Shell常用技巧(五) https://zhuanlan.zhihu.com/p/73451771 1. 变量:在awk中变量无须定义即可使用,变量在赋值时即已经完成了定义.变量的类型可 ...

  9. VisualStudioCode网页开发常用插件

    VS下载链接 提取码:usx8 VsCode扩展功能强大,在于他可以安装各种各样的插件来辅助开发.下面是我初学前端时常用的vscode开发插件 Chinese 汉化 Beautify 格式化javas ...

  10. (四)Resquest 知识点总结 (来自那些年的笔记)

    目录 URL和URI的区别 获取URL.URI 什么是HttpServletResquest 获取请求头中字段的内容 获取请求信息的数据 将客户机的请求变为一个流返回 常用的方法 request乱码问 ...