1、MySQL高可用 MHA

1.1.1 MHA工作原理

  1. MHA利用 SELECT 1 As Value 指令判断master服务器的健康性,一旦master 宕机,MHA 从宕机崩溃

的master保存二进制日志事件(binlog events)

  1. 识别含有最新更新的slave
  2. 应用差异的中继日志(relay log)到其他的slave
  3. 应用从master保存的二进制日志事件(binlog events)
  4. 提升一个slave为新的master
  5. 使其他的slave连接新的master进行复制

1.1.2 MHA 软件

MHA软件由两部分组成,Manager工具包和Node工具包

Manager工具包主要包括以下几个工具:


masterha_check_ssh 检查MHA的SSH配置状况
masterha_check_repl 检查MySQL复制状况
masterha_manger 启动MHA
masterha_check_status 检测当前MHA运行状态
masterha_master_monitor 检测master是否宕机
masterha_master_switch 故障转移(自动或手动)
masterha_conf_host 添加或删除配置的server信息
masterha_stop --conf=app1.cnf 停止MHA
masterha_secondary_check 两个或多个网络线路检查MySQL主服务器的可用

Node工具包:这些工具通常由MHA Manager的脚本触发,无需人为操作)主要包括以下几个工具:


save_binary_logs #保存和复制master的二进制日志
apply_diff_relay_logs #识别差异的中继日志事件并将其差异的事件应用于其他的slave
filter_mysqlbinlog #去除不必要的ROLLBACK事件(MHA已不再使用此工具)
purge_relay_logs #清除中继日志(不会阻塞SQL线程)

MHA配置文件:


global配置,为各application提供默认配置,默认文件路径 /etc/masterha_default.cnf
application配置:为每个主从复制集群

1.1.3 MHA 实现

1.1.3.1 在管理节点上安装两个包mha4mysql-manager和mha4mysql-node

说明:mha4mysql-manager-0.56-0.el6.noarch.rpm 不支持CentOS 8,只支持CentOS7 以下版本

mha4mysql-manager-0.58-0.el7.centos.noarch.rpm 支持MySQL5.7和MySQL8.0 ,但和CentOS8 版本上的Mariadb -10.3.17不兼容

#安装MHA可能提示以下错误,更新下epel软件库
[root@mha-manager ~]#yum -y install mha4mysql-node-0.58-0.el7.centos.noarch.rpm
[root@mha-manager ~]#yum -y install mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
...省略...
--> Finished Dependency Resolution
Error: Package: mha4mysql-manager-0.58-0.el7.centos.noarch (/mha4mysql-manager-0.58-0.el7.centos.noarch)
Requires: perl(MHA::NodeUtil)
Error: Package: mha4mysql-manager-0.58-0.el7.centos.noarch (/mha4mysql-manager-0.58-0.el7.centos.noarch)
Requires: perl(MHA::SlaveUtil)
Error: Package: mha4mysql-manager-0.58-0.el7.centos.noarch (/mha4mysql-manager-0.58-0.el7.centos.noarch)
Requires: perl(MHA::NodeConst)
Error: Package: mha4mysql-manager-0.58-0.el7.centos.noarch (/mha4mysql-manager-0.58-0.el7.centos.noarch)
Requires: perl(MHA::BinlogManager)
Error: Package: mha4mysql-manager-0.58-0.el7.centos.noarch (/mha4mysql-manager-0.58-0.el7.centos.noarch)
Requires: mha4mysql-node >= 0.54
You could try using --skip-broken to work around the problem
You could try running: rpm -Va --nofiles --nodigest
[root@mha-manager ~]#yum -y install epel-release
[root@mha-manager ~]#yum clean all
[root@mha-manager ~]#yum makecache

1.1.3.2 在所有MySQL服务器上安装mha4mysql-node包

此包支持Centos6,7,8

[root@master ~]#yum -y install mha4mysql-node-0.58-0.el7.centos.noarch.rpm
[root@slave1 ~]#yum -y install mha4mysql-node-0.58-0.el7.centos.noarch.rpm
[root@slave2 ~]#yum -y install mha4mysql-node-0.58-0.el7.centos.noarch.rpm

1.1.3.3 在所有节点实现相互之间ssh key 验证

[root@mha-manager ~]#ssh-keygen
[root@mha-manager ~]#ssh-copy-id 127.0.0.1
[root@mha-manager ~]#rsync -av .ssh 10.0.0.8:/root/
[root@mha-manager ~]#rsync -av .ssh 10.0.0.18:/root/
[root@mha-manager ~]#rsync -av .ssh 10.0.0.28:/root/

1.1.3.4 在管理节点创建配置文件

注意:此文件的行尾不要加空格等符号

[root@mha-manager ~]#mkdir /etc/mastermha/
[root@mha-manager ~]#vim /etc/mastermha/app1.cnf
[server default]
user=mhauser #用于远程连接MySQL所有节点的用户,需要有管理员的权限
password=123456
manager_workdir=/data/mastermha/app1/ #目录会自动生成,无需手动创建
manager_log=/data/mastermha/app1/manager.log
remote_workdir=/data/mastermha/app1/
ssh_user=root #用于实现远程ssh基于KEY的连接,访问二进制日志
repl_user=repluser #主从复制的用户信息
repl_password=123456
ping_interval=1 #健康性检查的时间间隔
master_ip_failover_script=/usr/local/bin/master_ip_failover #切换VIP的perl脚本
report_script=/usr/local/bin/sendmail.sh #当执行报警脚本
check_repl_delay=0 #默认值为1,表示如果slave中从库落后主库relay log超过100M,主库不会选择这个从库为新的master,因为这个从库进行恢复需要很长的时间.通过设置参数check_repl_delay=0,mha触发主从切换时会忽略复制的延时,对于设置candidate_master=1的从库非常有用,这样确保这个从库一定能成为最新的master
master_binlog_dir=/data/mysql/ #指定二进制日志存放的目录,mha4mysql-manager-0.58必须指
定,之前版本不需要指定
[server1]
hostname=10.0.0.8
candidate_master=1
[server2]
hostname=10.0.0.18
candidate_master=1 #设置为优先候选master,即使不是集群中事件最新的slave,也会优先当master
[server3]
hostname=10.0.0.28

说明:主库宕机谁来接管新的master

1、所有从节点日志都是一致的, 默认会已配置的顺序去选择一个新主
2、从节点日志不一致,自动选择最接近主库的从库充当新主
3、如果对于某个节点设置了权重(candidate_master=1),权重节点优先选择,但此节点日志落后主库100M也不会被选择。可以配合check_repl_delay=0,关闭日志量的检查,强制选择候选节点

1.1.3.5 相关脚本

[root@mha-manager ~]#cat  /usr/local/bin/sendmail.sh
#!/bin/bash
echo "MYSQL is down" | mail -s "MHA warning" 37******@qq.com
[root@mha-manager ~]#chmod +x /usr/local/bin/sendmail.sh
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
my (
$command, $ssh_user, $orig_master_host, $orig_master_ip,
$orig_master_port, $new_master_host, $new_master_ip, $new_master_port
);
#执行时必须删除下面三行注释
my $vip = '10.0.0.100/24';#设置Virtual IP
my $gateway = '10.0.0.254';#网关Gateway IP
my $interface = 'eth0'; #指定VIP所在网卡
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig $interface:$key $vip;/sbin/arping -I
$interface -c 3 -s $vip $gateway >/dev/null 2>&1";
my $ssh_stop_vip = "/sbin/ifconfig $interface:$key down";
GetOptions(
'command=s' => \$command,
'ssh_user=s' => \$ssh_user,
'orig_master_host=s' => \$orig_master_host,
'orig_master_ip=s' => \$orig_master_ip,
'orig_master_port=i' => \$orig_master_port,
'new_master_host=s' => \$new_master_host,
'new_master_ip=s' => \$new_master_ip,
'new_master_port=i' => \$new_master_port,
);
exit &main();
sub main {
print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
if ( $command eq "stop" || $command eq "stopssh" ) {
# $orig_master_host, $orig_master_ip, $orig_master_port are passed.
# If you manage master ip address at global catalog database,
# invalidate orig_master_ip here.
my $exit_code = 1;
eval {
print "Disabling the VIP on old master: $orig_master_host \n";
&stop_vip();
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {
# all arguments are passed.
# If you manage master ip address at global catalog database,
# activate new_master_ip here.
# You can also grant write access (create user, set read_only=0, etc) here.
my $exit_code = 10;
eval {
print "Enabling the VIP - $vip on the new master - $new_master_host \n";
&start_vip();
$exit_code = 0;
};
if ($@) {
warn $@;
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
print "Checking the Status of the script.. OK \n";
`ssh $ssh_user\@$orig_master_host \" $ssh_start_vip \"`;
exit 0;
}
else {
&usage();
exit 1;
} }
# A simple system call that enable the VIP on the new master
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
print
"Usage: master_ip_failover --command=start|stop|stopssh|status --
orig_master_host=host --orig_master_ip=ip --orig_master_port=port --
new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}
[root@mha-manager ~]#chmod +x /usr/local/bin/master_ip_failover

1.1.3.6 MySQL主从配置

#配置master
[root@master ~]#mkdir /data/mysql
[root@master ~]#chown -R mysql.mysql /data/mysql/
[root@master ~]#vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
server-id=8
log-bin=/data/mysql/master8

mysql> create user repluser@'10.0.0.%' identified by '123456';
mysql> grant replication slave on *.* to repluser@'10.0.0.%';
mysql> create user mhauser@'10.0.0.%' identified by '123456';
mysql> grant all on *.* to mhauser@'10.0.0.%';

#配置VIP
[root@master ~]#ifconfig eth0:1 10.0.0.100/24

#配置slave
[root@slave1 ~]#mkdir /data/mysql;chown -R mysql.mysql /data/mysql
[root@slave1 ~]#vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
server-id=18
log-bin=/data/mysql/masterbin18

[root@slave2 ~]#mkdir /data/mysql;chown -R mysql.mysql /data/mysql
[root@slave2 ~]#vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
server-id=28
log-bin=/data/mysql/masterbin18

mysql> CHANGE MASTER TO MASTER_HOST='10.0.0.8', MASTER_USER='repluser', MASTER_PASSWORD='123456', MASTER_LOG_FILE='master8.000002', MASTER_LOG_POS=156;
mysql> start slave;

1.1.3.7 检查MHA的环境

# MHA环境检查
[root@mha-manager ~]#masterha_check_ssh --conf=/etc/mastermha/app1.cnf
[root@mha-manager ~]#masterha_check_repl --conf=/etc/mastermha/app1.cnf
#查看状态
[root@mha-manager ~]#masterha_check_status --conf=/etc/mastermha/app1.cnf
app1 is stopped(2:NOT_RUNNING).

1.1.3.8 启动MHA

#开启MHA,默认是前台运行,生产环境一般为后台执行
nohup masterha_manager --conf=/etc/mastermha/app1.cnf &> /dev/null

[root@mha-manager ~]#masterha_manager --conf=/etc/mastermha/app1.cnf
Tue May 3 21:46:24 2022 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue May 3 21:46:24 2022 - [info] Reading application default configuration from /etc/mastermha/app1.cnf..
Tue May 3 21:46:24 2022 - [info] Reading server configuration from /etc/mastermha/app1.cnf..

#查看状态
[root@mha-manager ~]#masterha_check_status --conf=/etc/mastermha/app1.cnf
app1 (pid:5180) is running(0:PING_OK), master:10.0.0.8

1.1.3.9 模拟master故障

[root@master ~]#systemctl stop mysqld.service
#master down后,MHA自动退出,并收到告警邮件
[root@mha-manager </sub>]#masterha_manager --conf=/etc/mastermha/app1.cnf
Tue May 3 21:46:24 2022 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue May 3 21:46:24 2022 - [info] Reading application default configuration from /etc/mastermha/app1.cnf..
Tue May 3 21:46:24 2022 - [info] Reading server configuration from /etc/mastermha/app1.cnf..
Creating /data/mastermha/app1 if not exists.. ok.
Checking output directory is accessible or not..
ok.
Binlog found at /data/mysql/, up to master8.000002
Tue May 3 21:51:42 2022 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue May 3 21:51:42 2022 - [info] Reading application default configuration from /etc/mastermha/app1.cnf..
Tue May 3 21:51:42 2022 - [info] Reading server configuration from /etc/mastermha/app1.cnf..
[root@mha-manager ~]#
[root@mha-manager ~]#cat /data/mastermha/app1/manager.log

# VIP漂移至新的master上
[root@slave1 ~]#ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:3a:e8:98 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.18/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 10.0.0.100/24 brd 10.0.0.255 scope global secondary eth0:1