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. tensorflow学习 从入门到实战(转)

    原文作者:zhaozhengcoder链接:https://www.jianshu.com/p/27a2fb320934來源:简书简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处. ...

  2. Laravel 别名Redis 与 Redis 扩展冲突

    use Redis; //通过别名引用会报错 今天尝试使用了 Laravel 的 redis 结果报了如下错误. Non-static method Redis::xxx() cannot be ca ...

  3. JEECG中修改时间相关自定义定时器

    JEECG中使用,如下: @InitBinder public void initBinder(ServletRequestDataBinder binder) { binder.registerCu ...

  4. 【Leetcode_easy】705. Design HashSet

    problem 705. Design HashSet 题意: solution1: class MyHashSet { public: /** Initialize your data struct ...

  5. 最新 开创java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.开创等10家互联网公司的校招Offer,因为某些自身原因最终选择了开创.6.7月主要是做系统复习.项目复盘.LeetCode ...

  6. Jmeter CSV操作

    统计行号列号 import java.io.BufferedReader; import java.io.FileReader; import java.io.File; print("== ...

  7. javascript当中火狐的firebug如何单步调试程序?

    [学习笔记] 2)火狐的firebug如何单步调试程序 马克-to-win:火狐中:工具/web 开发者/调试器,开始时没有文件,在浏览器当中点刷新按钮,文件就加载进来了. 文章转载自原文:https ...

  8. 开源定时任务框架Quartz(一)

    简介 Quartz是OpenSymphony开源组织的一个开源项目,定时任务框架,纯Java语言实现,最新版本为2.3.0. 设计模式 Quartz中使用的设计模式如下: 1.Builder模式 2. ...

  9. LC 21. Merge Two Sorted Lists

    题目描述 Merge two sorted linked lists and return it as a new list. The new list should be made by splic ...

  10. php之简单算法

    选择排序 方式:先让第一位与其他位比较大小找到最小的数字,然后是第二位与除第一位的其他位比较大小找出第二位,依此类推 $arr = [2,45,12,67,33,5,23,132,46]; for ( ...