MHA目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司 youshimaton(现就职于 Facebook公司)开发,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件。在MySQL故障切换过程中,MHA能做到在0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,MHA能在最大程度上保证数据的一致性,以达到真正意义上的高可用。
MHA Manager(管理节点)
MHA Node(数据节点)
■ 自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据的不丢失
■ 使用 MySQL55的半同步复制,可以大大降低数据丢失的风险
传统的MySQL主从架构存在问题
通过 MHA 监控 MySQL 数据库在故障时进行自动切换,不影响业务。
服务器 | IP | 作用 |
---|---|---|
MHA-manager | 20.0.0.24 | 管理节点,安装 manager 组件 |
Mysql1 | 20.0.0.21 | Master 节点,安装 node 组件 |
Mysql2 | 20.0.0.22 | Slave 节点,安装 node 组件 |
Mysql3 | 20.0.0.23 | Slave 节点,安装 node 组件 |
这里操作系统是 CentOS7 版本,所以这里下载 MHA 版本是 0.57 版本。
在三台 MySQL 节点上分别安装数据库,版本为5.7.20
详情:https://blog.csdn.net/zg_66/article/details/108320586 中的MySQL安装部分
搭建好MySQL主从复制环境
详情:https://blog.csdn.net/zg_66/article/details/108578383
注: YUM环境必须是现网源
[root@MHA-manager ~]# yum install epel-release --nogpgcheck -y ###扩展源
[root@mha-manager ~]# yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN
[root@Mysql1 ~]# tar zxvf mha4mysql-node-0.57.tar.gz
[root@Mysql1 ~]# cd mha4mysql-node-0.57
[root@Mysql1 mha4mysql-node-0.57]# perl Makefile.PL
[root@Mysql1 mha4mysql-node-0.57]# make
[root@Mysql1 mha4mysql-node-0.57]# make install
[root@MHA-manager ~]# tar zxvf mha4mysql-manager-0.57.tar.gz
[root@MHA-manager ~]# cd mha4mysql-manager-0.57/
[root@MHA-manager mha4mysql-manager-0.57]# perl Makefile.PL
[root@MHA-manager mha4mysql-manager-0.57]# make
[root@MHA-manager mha4mysql-manager-0.57]# make install
注释:
manager 安装后在/usr/local/bin 下面会生成几个工具,主要包括以下几个:
masterha_check_ssh 检查 MHA 的 SSH 配置状况
masterha_check_repl 检查 MySQL 复制状况
masterha_manger 启动 manager的脚本
masterha_check_status 检测当前 MHA 运行状态
masterha_master_monitor 检测 master 是否宕机
masterha_master_switch 控制故障转移(自动或者手动)
masterha_conf_host 添加或删除配置的 server 信息
masterha_stop 关闭manager
[root@MHA-manager ~]# cp -ra /root/mha4mysql-manager-0.57/samples/scripts /usr/local/bin ###拷贝后会有四个执行文件
[root@atlas ~]# ll /usr/local/bin/scripts/
总用量 32
-rwxr-xr-x 1 mysql mysql 3648 5 月 31 2015 master_ip_failover ###自动切换时 VIP 管理的脚本
-rwxr-xr-x 1 mysql mysql 9872 5 月 25 09:07 master_ip_online_change ###在线切换时 vip 的管理
-rwxr-xr-x 1 mysql mysql 11867 5 月 31 2015 power_manager ###故障发生后关闭主机的脚本
-rwxr-xr-x 1 mysql mysql 1360 5 月 31 2015 send_report ###因故障切换后发送报警的脚本
② 复制上述的自动切换时 VIP 管理的脚本到/usr/local/bin 目录,这里使用脚本管理 VIP
[root@MHA-manager ~]# cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
[root@MHA-manager ~]#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 = '20.0.0.200';
my $brdc = '20.0.0.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";
}
[root@MHA-manager ~]# mkdir /etc/masterha
[root@MHA-manager ~]# cp /root/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/
[root@MHA-manager ~]# 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/master_ip_online_change
password=manager
ping_interval=1
remote_workdir=/tmp
repl_password=123
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 20.0.0.22 -s 20.0.0.23
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=20.0.0.21
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=20.0.0.22
port=3306
[server3]
hostname=20.0.0.23
port=3306
'-------------------------------------------------配置文件解析---------------------------------------------------------------------'
[server default]
manager_workdir=/var/log/masterha/app1.log ##manager工作目录
manager_log=/var/log/masterha/app1/manager.log #manager日志
master_binlog_dir=/usr/local/mysql/data/ #master保存binlog的位置,这里的路径要与master里配置的binlog的路径一致,以便mha能找到
#master_ip_failover_script= /usr/local/bin/master_ip_failover ###设置自动failover时候的切换脚本,也就是上边的哪个脚本
master_ip_online_change_script= /usr/local/bin/master_ip_online_change ###设置手动切换时候的切换脚本
password=manager ###设置mysql中root用户的密码,这个密码是前文中创建监控用户的那个密码
user=mha ###设置监控用户root
ping_interval=1 ###设置监控主库,发送ping包的时间间隔,默认是3秒,尝试三次没有回应的时候自动进行railover
remote_workdir=/tmp ###设置远端mysql在发生切换时binlog的保存位置
repl_password=123 ###设置复制用户的密码
repl_user=myslave ###设置复制用户的用户
report_script=/usr/local/send_report ###设置发生切换后发送的报警的脚本
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 20.0.0.22 -s 20.0.0.23
shutdown_script="" ###设置故障发生后关闭故障主机脚本(该脚本的主要作用是关闭主机放在发生脑裂,这里没有使用)
ssh_user=root ###设置ssh的登录用户名
[server1]
hostname=20.0.0.21
port=3306
[server2]
hostname=20.0.0.22
port=3306
candidate_master=1 ###设置为候选master,如果设置该参数以后,发生主从切换以后将会将此从库提升为主库,即使这个主库不是集群中事件最新的slave
check_repl_delay=0 ###默认情况下如果一个slave落后master 100M的relay logs的话,MHA将不会选择该slave作为一个新的master,因为对于这个slave的恢复需要花费很长时间,通过设置check_repl_delay=0,MHA触发切换在选择一个新的master的时候将会忽略复制延时,这个参数对于设置了candidate_master=1的主机非常有用,因为这个候选主在切换的过程中一定是新的master
[server3]
hostname=20.0.0.23
port=3306
[root@MHA-manager ~]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
[root@mha-manager ~]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
Wed Oct 21 05:19:30 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Oct 21 05:19:30 2020 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Wed Oct 21 05:19:30 2020 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Wed Oct 21 05:19:30 2020 - [info] Starting SSH connection tests..
Wed Oct 21 05:19:31 2020 - [debug]
Wed Oct 21 05:19:30 2020 - [debug] Connecting via SSH from [email protected](20.0.0.21:22) to [email protected](20.0.0.22:22)..
Wed Oct 21 05:19:31 2020 - [debug] ok.
Wed Oct 21 05:19:31 2020 - [debug] Connecting via SSH from [email protected](20.0.0.21:22) to [email protected](20.0.0.23:22)..
Wed Oct 21 05:19:31 2020 - [debug] ok.
Wed Oct 21 05:19:32 2020 - [debug]
Wed Oct 21 05:19:31 2020 - [debug] Connecting via SSH from [email protected](20.0.0.22:22) to [email protected](20.0.0.21:22)..
Wed Oct 21 05:19:31 2020 - [debug] ok.
Wed Oct 21 05:19:31 2020 - [debug] Connecting via SSH from [email protected](20.0.0.22:22) to [email protected](20.0.0.23:22)..
Wed Oct 21 05:19:32 2020 - [debug] ok.
Wed Oct 21 05:19:33 2020 - [debug]
Wed Oct 21 05:19:31 2020 - [debug] Connecting via SSH from [email protected](20.0.0.23:22) to [email protected](20.0.0.21:22)..
Wed Oct 21 05:19:32 2020 - [debug] ok.
Wed Oct 21 05:19:32 2020 - [debug] Connecting via SSH from [email protected](20.0.0.23:22) to [email protected](20.0.0.22:22)..
Wed Oct 21 05:19:32 2020 - [debug] ok.
Wed Oct 21 05:19:33 2020 - [info] All SSH connection tests passed successfully.
----------------------------mysql5.7注意--------------------------------------
请注释/etc/my.cnf 中 【client】下 #default-character-set=utf8
在所有数据库中建立以下指令软连接
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
[root@MHA-manager ~]# masterha_check_repl -conf=/etc/masterha/app1.cnf
Tue Nov 26 23:10:29 2019 - [info] Slaves settings check done.
Tue Nov 26 23:10:29 2019 - [info]
...
MySQL Replication Health is OK. ###出现ok就行,这边很容易出现报错
'###注意:第一次配置需要去master上手动开启虚拟IP'
[root@Mysql1 ~]# /sbin/ifconfig ens33:1 20.0.0.200/24
[root@mysql1 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 20.0.0.25 netmask 255.255.255.0 broadcast 20.0.0.255
inet6 fe80::3069:1a3d:774b:18f prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:11:0d:16 txqueuelen 1000 (Ethernet)
RX packets 79297 bytes 81001774 (77.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 34637 bytes 5776599 (5.5 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 20.0.0.200 netmask 255.255.255.0 broadcast 20.0.0.255
ether 00:0c:29:11:0d:16 txqueuelen 1000 (Ethernet)
[root@MHA-manager ~]# 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 &
[4] 77095
注释:
–remove_dead_master_conf 该参数代表当发生主从切换后,老的主库的 ip 将会从配置文件中移除。
–manger_log 日志存放位置。
–ignore_last_failover 在缺省情况下,如果 MHA 检测到连续发生宕机,且两次宕机间
隔不足 8 小时的话,则不会进行 Failover,之所以这样限制是为了避免 ping-pong 效应。该
参数代表忽略上次 MHA 触发切换产生的文件,默认情况下,MHA 发生切换后会在日志记
目录,也就是上面设置的日志 app1.failover.complete 文件,下次再次切换的时候如果发现
该目录下存在该文件将不允许触发切换,除非在第一次切换后收到删除该文件,为了方便,
这里设置为–ignore_last_failover。
'在mysql1上'
[root@mysql1 ~]# systemctl stop mysqld
'在mysql2上'
[root@mysql2 ~]# ifconfig
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 20.0.0.200 netmask 255.0.0.0 broadcast 20.255.255.255
ether 00:0c:29:9d:2f:ca txqueuelen 1000 (Ethernet)
mysql> show master status;
±----------±---------±-------------±-----------------±------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
±----------±---------±-------------±-----------------±------------------+
| on.000001 | 154 | | | |
±----------±---------±-------------±-----------------±------------------+
1 row in set (0.00 sec)
'在manager上'
[root@localhost ~]# 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 &
[1] 29011
[root@localhost ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:29011) is running(0:PING_OK), master:20.0.0.22 ###发现mysql2接管了主服务器
动态查看
[root@mha-manager ~]# tailf /var/log/masterha/app1/manager.log
...省略部分内容
Master 20.0.0.21(20.0.0.21:3306) is down!
Check MHA Manager logs at mha-manager:/var/log/masterha/app1/manager.log for details.
Started automated(non-interactive) failover.
Invalidated master IP address on 20.0.0.21(20.0.0.21:3306)
The latest slave 20.0.0.22(20.0.0.22:3306) has all relay logs for recovery.
Selected 20.0.0.22(20.0.0.22:3306) as a new master.
20.0.0.22(20.0.0.22:3306): OK: Applying all logs succeeded.
20.0.0.22(20.0.0.22:3306): OK: Activated master IP address.
20.0.0.23(20.0.0.23:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
20.0.0.23(20.0.0.23:3306): OK: Applying all logs succeeded. Slave started, replicating from 20.0.0.22(20.0.0.22:3306)
20.0.0.22(20.0.0.22:3306): Resetting slave info succeeded.
Master failover to 20.0.0.22(20.0.0.22:3306) completed successfully.
'在mysql1上'
[root@mysql1 ~]# systemctl start mysqld
[root@mysql1 ~]# mysql -uroot -pabc123
mysql> change master to master_host=‘20.0.0.22’,master_user=‘myslave’,master_password=‘123’,master_log_file=‘on.000001’,master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
'在manager服务器上'
修改配置文件:(再把这个记录添加进去,因为它检测掉失效时候会自动消失)
[root@mha-manager ~]# vi /etc/masterha/app1.cnf
[server1]
hostname=20.0.0.21
port=3306
'启动manager'
[root@mha-manager ~]# 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 &
[2] 29603
'在mysql2上'
mysql> create database school;
Query OK, 1 row affected (0.00 sec)
'在mysql1和mysql3上查看'
mysql> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
| test_db |
±-------------------+
6 rows in set (0.00 sec)
成功