mysql集群MHA高可用配置详解
一:mysql概述
1.1:什么是MHA
- 日本DeNA公司 youshimaton(现就职于 Facebook公司)开发
- 一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件
- 支持故障切换
- 在MySQL故障切换过程中,MHA能做到在0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,MHA能在最大程度上保证数据的一致性,以达到真正意义上的高可用
- MHA还提供在线主库切换的功能,能够安全地切换当前运行的主库到一个新的主库中(通过将从库提升为主库),大概0.5-2秒内即可完成
1.2:MHA的组成
- MHA Manager(管理节点)
用来接收外部信号,监控下方数据节点的工作状态 - MHA Node(数据节点)
工作的单位,负责具体的工作
1.3:MHA特点
- 自动故障切换过程中,MHA试图从宕机的主服务器上保持二进制日志,最大程度的保证数据不丢失
- 使用半同步复制,可以大大降低数据丢失的风险
- 目前MHA支持一主多从架构,至少三台服务器,即一主两从
1.4:MHA的由来
传统的mysql主从架构存在的问题
单点故障 master只有一台,所以当出现单点故障的时候,整个服务器群集就会瘫痪掉
为了解决这种情况,我们需要在主服务器宕机的时候,重新建立一台主服务器,负责监控等工作
二:MHA配置
2.1:实验环境介绍
VMware软件
一台centos7作为MHA
三台centos7作为mysql服务器
搭建好mysql主从复制环境(主从复制参见之前的博客)
2.2:实验架构图
2.3:实验目的
通过MHA监控MySQL数据库,在故障时自动进行切换,不影响业务
当主库失效时,备选主库自动成为主库
2.4:实验步骤
2.4.1:所有节点上安装环境包和node组件
- 安装环境包,以manager为例
[root@mha ~]# yum install epel-release --nogpgcheck -y
[root@mha ~]# yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN
- 安装node组件,以salve为例
[root@slave01 ~]# ls
anaconda-ks.cfg 公共 视频 文档 音乐
initial-setup-ks.cfg mha4mysql-node-0.57.tar.gz 模板 图片 下载 桌面
[root@slave01 ~]# tar zxvf mha4mysql-node-0.57
[root@slave01 ~]# cd mha4mysql-node-0.57/
[root@slave01 mha4mysql-node-0.57]# ls
AUTHORS blib debian lib Makefile.PL META.yml MYMETA.yml README t
bin COPYING inc Makefile MANIFEST MYMETA.json pm_to_blib rpm
[root@slave01 mha4mysql-node-0.57]# perl Makefile.PL
[root@slave01 mha4mysql-node-0.57]# make && make install
- 在MHA-manager节点上安装manager组件
[root@mha ~]# ls
anaconda-ks.cfg mha4mysql-manager-0.57 mha4mysql-node-0.57 公共 视频 文档 音乐
initial-setup-ks.cfg mha4mysql-manager-0.57.tar.gz mha4mysql-node-0.57.tar.gz 模板 图片 下载 桌面
[root@mha ~]# tar zxvf mha4mysql-manager-0.57.tar.gz
[root@mha ~]# cd mha4mysql-manager-0.57/
[root@mha mha4mysql-manager-0.57]# ls
AUTHORS blib debian lib Makefile.PL META.yml MYMETA.yml README samples tests
bin COPYING inc Makefile MANIFEST MYMETA.json pm_to_blib rpm t
[root@mha mha4mysql-manager-0.57]# perl Makefile.PL
[root@mha mha4mysql-manager-0.57]# make && make install
-
manager安装后在/usr/local/bin 下面会生成几个工具,主要包括以下几个:
masterha_check_ssh 检查MHA的SSH配置状况
masterha_check_repl 检查MySQL的复制状况
masterha_manager 启动manager的脚本
masterha_check_status 检查MHA的运行状态
masterha_master_monitor 检查master是否宕机
masterha_master_swith 控制故障切换
masterha_conf_host 添加或删除配置的server信息
masterha_stop 关闭manager -
node安装后也会在/usr/local/bin 下面生成几个工具(这些工具通常由MHA-manager的脚本触发,无须人为操作)
save_binary_logs 保存和复制master的二进制日志
apply_diff_relay_log 识别差别中继日志事件并将其差异的事件应用于其他slave
purge_relay_log 清除中继日志
filter_mysqlbinlog
2.4.2:配置所有节点之间的无密码认证
- 在manager上配置到所有数据库节点的无密码认证(192.168.209.134)
ssh-keygen -t rsa //一路回车
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.209.145
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.209.146
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.209.147
- 在master上配置到slave01和slave02的无密码认证(192.168.209.145)
ssh-keygen -t rsa //一路回车
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.209.146
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.209.147
- 在slave01上配置到master和slave02的无密码认证(192.168.209.146)
ssh-keygen -t rsa //一路回车
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.209.145
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.209.147
- 在slave02上配置到master和slave01的无密码认证(192.168.209.147)
ssh-keygen -t rsa //一路回车
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.209.145
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.209.146
2.4.3:MHA的配置
- 优化执行路径
[root@mha ~]# cp -ra /root/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
//copy后会有四个执行文件
master_ip_failover 自动切换VIP管理的脚本
master_ip_online_change 在线切换VIP
power_manager 故障发生后关闭主机的脚本
send_report 故障发生后发送报警的脚本
[root@mha ~]# cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
- 修改master_ip_failover脚本
[root@mha ~]# vim /usr/local/bin/master_ip_failover
#!/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 = '192.168.209.200';
my $brdc = '192.168.209.255';
my $ifdev = 'ens33';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
my $exit_code = 0;
#my $ssh_start_vip = "/usr/sbin/ip addr add $vip/24 brd $brdc dev $ifdev label $ifdev:$key;/usr/sbin/arping -q -A -c 1 -I $ifdev $vip;iptables -F;";
#my $ssh_stop_vip = "/usr/sbin/ip addr del $vip/24 dev $ifdev label $ifdev:$key";
##################################################################################
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" ) {
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" ) {
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";
exit 0;
}
else {
&usage();
exit 1;
}
}
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";
}
- 修改配置文件app1.cnf
[root@mha ~]# mkdir /etc/masterha
[root@mha ~]# cp /root/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha
[root@mha ~]# vim /etc/masterha/app1.cnf
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/usr/local/mysql/data
master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script=/usr/local/bin/scripts/master_ip_online_change
user=mha
password=123123
ping_interval=1
remote_workdir=/tmp
repl_user=myslave
repl_password=123123
report_script=/usr/local/send_report
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.209.146 -s 192.168.209.147
shutdown_script=""
ssh_user=root
[server1]
hostname=192.168.209.145
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.209.146
port=3306
[server3]
hostname=192.168.209.147
port=3306
- 测试ssh无密码认证,如果正常,最后会输出successfully
[root@mha ~]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
......
Tue Sep 8 10:23:15 2020 - [info] All SSH connection tests passed successfully.
- 测试mysql的主从复制,如果正常,最后会输出healthy is ok
[root@mha ~]# masterha_check_repl -conf=/etc/masterha/app1.cnf
......
MySQL Replication Health is OK.
注意:第一次配置需在master上手动开启虚拟VIP
ifconfig ens33:1 192.168.209.200/24
- 启动MHA
[root@mha ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &
- 查看MHA的状态或者日志文件
[root@mha ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:8454) is running(0:PING_OK), master:192.168.209.145
[root@mha ~]# cat /var/log/masterha/app1/manager.log
......
2.4.4:故障模拟
- 启动监控观察日志记录
[root@mha ~]# tailf /var/log/masterha/app1/manager.log
- 关闭master
[root@master ~]# pkill -9 mysqld
2.5:实验验证
在从服务器上(slave01)查看VIP有没有自动切换过来
[root@slave01 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.209.146 netmask 255.255.255.0 broadcast 192.168.209.255
inet6 fe80::eefc:1727:92ef:63f2 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1c:67:bb txqueuelen 1000 (Ethernet)
RX packets 1809 bytes 215257 (210.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1242 bytes 271843 (265.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.209.200 netmask 255.255.255.0 broadcast 192.168.209.255
ether 00:0c:29:1c:67:bb txqueuelen 1000 (Ethernet)
可以看到自动切换成功
在manager上查看日志记录
----- Failover Report -----
app1: MySQL Master failover 192.168.209.145(192.168.209.145:3306) to 192.168.209.146(192.168.209.146:3306) succeeded
Master 192.168.209.145(192.168.209.145:3306) is down!
Check MHA Manager logs at mha:/var/log/masterha/app1/manager.log for details.
Started automated(non-interactive) failover.
Invalidated master IP address on 192.168.209.145(192.168.209.145:3306)
The latest slave 192.168.209.146(192.168.209.146:3306) has all relay logs for recovery.
Selected 192.168.209.146(192.168.209.146:3306) as a new master.
192.168.209.146(192.168.209.146:3306): OK: Applying all logs succeeded.
192.168.209.146(192.168.209.146:3306): OK: Activated master IP address.
192.168.209.147(192.168.209.147:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
192.168.209.147(192.168.209.147:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.209.146(192.168.209.146:3306)
192.168.209.146(192.168.209.146:3306): Resetting slave info succeeded.
Master failover to 192.168.209.146(192.168.209.146:3306) completed successfully.
至此整个实验配置完成!