跳转至

28. MHA搭建

30. 高可用-MHA搭建

1. MHA软件构成

#manager工具包

1.masterha_check_ssh        #检查mha的ssh配置状况
2.masterha_check_repl       #检查mysql复制状况
3.masterha_manager          #启动mha
4.masterha_check_status      #检查当前mha运行状态
5.masterha_master_monitor   #检测master是否宕机
6.masterha_master_switch     #控制故障转移(自动/手动)
7.masterha_conf_host        #添加/删除配置的server信息
#node工具包——这些工具通常由mha manager脚本触发,无需人为操作)

1.save_binary_logs          #保存和复制master的二进制日志
2.apply_diff_relay_logs     #识别差异的中继日志事件并将其差异的事件应用于其他的
3.slave filter_mysqlbinlog   #取出不必要的rollback事件(mha已不再使用这个工具)
4.purge_relay_logs          #清除中继日志(不会阻塞SQL线程)

2. 下载mha软件

1.浏览器访问https://github.com/yoshinorim/

2.点击manager,然后点击右侧的最新软件包

3.选择rpm包点击下载

4.点击node后,再点击右侧的最新软件包

5.选择rpm包点击下载

3. 上传rpm包

cd /server/tools
ls /server/tools
[root@161 tools]# cd /server/tools
[root@161 tools]# ls /server/tools
mha4mysql-manager-0.58-0.el7.centos.noarch.rpm  mha4mysql-node-0.58-0.el7.centos.noarch.rpm

4. 所有节点安装依赖包

yum install perl-DBD-MySQL -y

5. 所有节点安装node软件

rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
[root@151 tools]# rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:mha4mysql-node-0.58-0.el7.centos ################################# [100%]
[root@151 tools]#

6. 主库创建mha用户

#登录主库后
#151
grant all privileges on *.* to mha@'192.168.178.%' identified by 'mha';
flush privileges;

7. 选择某一个从库(162)安装manager依赖包

#162
yum install -y perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes

8. 选择某一个从库(162)安装manager

#162
rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
[root@162 tools]# rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:mha4mysql-manager-0.58-0.el7.cent################################# [100%]
[root@162 tools]#

9. 创建配置文件目录

#162
mkdir -p /etc/mha

10. 创建日志目录

#162
mkdir -p /var/log/mha/app1

11. 编辑mha配置文件

#162
vim /etc/mha/app1.cnf
[server default]
manager_log=/var/log/mha/app1/manager
manager_workdir=/var/log/mha/app1
master_binlog_dir=/data/mysql           #主库二进制日志目录
user=mha
password=mha
ping_interval=2
repl_user=repl
repl_password=123
ssh_user=root

[server1]
hostname=192.168.178.151
port=3306
[server2]
hostname=192.168.178.161
port=3306
[server3]
hostname=192.168.178.162
port=3306

12. manager额外参数介绍

#1.设置监控主库,发送ping包的时间间隔,尝试三次没有回应的时候自动进行failover
ping_interval=1

#功能类似如下命令
[root@162 tools]# mysqladmin ping
mysqld is alive
[root@162 tools]#
#2.设置从库为候选master,如果设置该参数后,发生主从切换时将此从库提升为主库,即使这个主库不是集群中最新的
candidate_master=1
#3.默认情况下,如果一个从库落后主库100M的relay logs的话,mha将不会选择该从库作为新的master,因为对于这个slave的恢复需要花费很长时间
check_repl_delay=0

13. 互信状态检查

#162
masterha_check_ssh --conf=/etc/mha/app1.cnf
[root@162 tools]# masterha_check_ssh --conf=/etc/mha/app1.cnf
Wed Apr 28 22:37:37 2021 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Apr 28 22:37:37 2021 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Wed Apr 28 22:37:37 2021 - [info] Reading server configuration from /etc/mha/app1.cnf..
Wed Apr 28 22:37:37 2021 - [info] Starting SSH connection tests..
Wed Apr 28 22:37:38 2021 - [debug]
Wed Apr 28 22:37:37 2021 - [debug]  Connecting via SSH from root@192.168.178.151(192.168.178.151:22) to root@192.168.178.161(192.168.178.161:22)..
Wed Apr 28 22:37:37 2021 - [debug]   ok.
Wed Apr 28 22:37:37 2021 - [debug]  Connecting via SSH from root@192.168.178.151(192.168.178.151:22) to root@192.168.178.162(192.168.178.162:22)..
Wed Apr 28 22:37:37 2021 - [debug]   ok.
Wed Apr 28 22:37:38 2021 - [debug]
Wed Apr 28 22:37:37 2021 - [debug]  Connecting via SSH from root@192.168.178.161(192.168.178.161:22) to root@192.168.178.151(192.168.178.151:22)..
Wed Apr 28 22:37:38 2021 - [debug]   ok.
Wed Apr 28 22:37:38 2021 - [debug]  Connecting via SSH from root@192.168.178.161(192.168.178.161:22) to root@192.168.178.162(192.168.178.162:22)..
Wed Apr 28 22:37:38 2021 - [debug]   ok.
Wed Apr 28 22:37:39 2021 - [debug]
Wed Apr 28 22:37:38 2021 - [debug]  Connecting via SSH from root@192.168.178.162(192.168.178.162:22) to root@192.168.178.151(192.168.178.151:22)..
Wed Apr 28 22:37:38 2021 - [debug]   ok.
Wed Apr 28 22:37:38 2021 - [debug]  Connecting via SSH from root@192.168.178.162(192.168.178.162:22) to root@192.168.178.161(192.168.178.161:22)..
Wed Apr 28 22:37:38 2021 - [debug]   ok.
Wed Apr 28 22:37:39 2021 - [info] All SSH connection tests passed successfully.
[root@162 tools]#

14. 主从状态检查

#162
masterha_check_repl --conf=/etc/mha/app1.cnf
[root@162 tools]# masterha_check_repl --conf=/etc/mha/app1.cnf
Wed Apr 28 22:38:10 2021 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Apr 28 22:38:10 2021 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Wed Apr 28 22:38:10 2021 - [info] Reading server configuration from /etc/mha/app1.cnf..
Wed Apr 28 22:38:10 2021 - [info] MHA::MasterMonitor version 0.58.
Wed Apr 28 22:38:12 2021 - [info] GTID failover mode = 1
Wed Apr 28 22:38:12 2021 - [info] Dead Servers:
Wed Apr 28 22:38:12 2021 - [info] Alive Servers:
Wed Apr 28 22:38:12 2021 - [info]   192.168.178.151(192.168.178.151:3306)
Wed Apr 28 22:38:12 2021 - [info]   192.168.178.161(192.168.178.161:3306)
Wed Apr 28 22:38:12 2021 - [info]   192.168.178.162(192.168.178.162:3306)
Wed Apr 28 22:38:12 2021 - [info] Alive Slaves:
Wed Apr 28 22:38:12 2021 - [info]   192.168.178.161(192.168.178.161:3306)  Version=5.7.20-log (oldest major version between slaves) log-bin:enabled
Wed Apr 28 22:38:12 2021 - [info]     GTID ON
Wed Apr 28 22:38:12 2021 - [info]     Replicating from 192.168.178.151(192.168.178.151:3306)
Wed Apr 28 22:38:12 2021 - [info]   192.168.178.162(192.168.178.162:3306)  Version=5.7.20-log (oldest major version between slaves) log-bin:enabled
Wed Apr 28 22:38:12 2021 - [info]     GTID ON
Wed Apr 28 22:38:12 2021 - [info]     Replicating from 192.168.178.151(192.168.178.151:3306)
Wed Apr 28 22:38:12 2021 - [info] Current Alive Master: 192.168.178.151(192.168.178.151:3306)
Wed Apr 28 22:38:12 2021 - [info] Checking slave configurations..
Wed Apr 28 22:38:12 2021 - [info]  read_only=1 is not set on slave 192.168.178.161(192.168.178.161:3306).
Wed Apr 28 22:38:12 2021 - [info]  read_only=1 is not set on slave 192.168.178.162(192.168.178.162:3306).
Wed Apr 28 22:38:12 2021 - [info] Checking replication filtering settings..
Wed Apr 28 22:38:12 2021 - [info]  binlog_do_db= , binlog_ignore_db=
Wed Apr 28 22:38:12 2021 - [info]  Replication filtering check ok.
Wed Apr 28 22:38:12 2021 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Wed Apr 28 22:38:12 2021 - [info] Checking SSH publickey authentication settings on the current master..
Wed Apr 28 22:38:12 2021 - [info] HealthCheck: SSH to 192.168.178.151 is reachable.
Wed Apr 28 22:38:12 2021 - [info]
192.168.178.151(192.168.178.151:3306) (current master)
 +--192.168.178.161(192.168.178.161:3306)
 +--192.168.178.162(192.168.178.162:3306)

Wed Apr 28 22:38:12 2021 - [info] Checking replication health on 192.168.178.161..
Wed Apr 28 22:38:12 2021 - [info]  ok.
Wed Apr 28 22:38:12 2021 - [info] Checking replication health on 192.168.178.162..
Wed Apr 28 22:38:12 2021 - [info]  ok.
Wed Apr 28 22:38:12 2021 - [warning] master_ip_failover_script is not defined.
Wed Apr 28 22:38:12 2021 - [warning] shutdown_script is not defined.
Wed Apr 28 22:38:12 2021 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.
[root@162 tools]#

15. 开启mha

#162
nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover </dev/null >/var/log/mha/app1/manager.log 2>&1 &
#参数说明
--conf=/etc/mha/app1.cnf    #指定配置文件
--remove_dead_master_conf   #自动删除故障节点
--ignore_last_failover      #一直能切换
[root@162 tools]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover </dev/null >/var/log/mha/app1/manager.log 2>&1 &
[1] 53844
[root@162 tools]#
[root@162 tools]#
[root@162 tools]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      7132/sshd
tcp6       0      0 :::3306                 :::*                    LISTEN      46545/mysqld
tcp6       0      0 :::22                   :::*                    LISTEN      7132/sshd
[root@162 tools]#
[root@162 tools]# ps -ef|grep mha
root      53844  51398  0 22:38 pts/0    00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover
root      54123  51398  0 22:39 pts/0    00:00:00 grep --color=auto mha
[root@162 tools]#

16. 查看mha状态

#162
masterha_check_status  --conf=/etc/mha/app1.cnf
[root@162 tools]# masterha_check_status  --conf=/etc/mha/app1.cnf
app1 (pid:53844) is running(0:PING_OK), master:192.168.178.151
[root@162 tools]#

最后更新: 2022-02-20 11:21:58