MHA实现MySQL主从的高可用
MHA:Master High Availability,对主节点进行监控,可实现自动故障转移至其它从节点;通过提升某一从节点为新的主节点,基于主从复制实现,还需要客户端配合实现,目前MHA主要支持一主多从的架构,要搭建MHA,要求一个复制集群中必须最少有三台数据库服务器,一主二从,即一台充当master,一台充当备用master,另外一台充当从库,出于机器成本的考虑,淘宝进行了改造,目前淘宝TMHA已经支持一主一从
MHA的工作原理:
1、MHA利用 SELECT 1 As Value 指令判断master服务器的健康性,一旦master 宕机,MHA 从宕机崩溃的master保存二进制日志事件(binlog events)
2、识别含有最新更新的slave
3、应用差异的中继日志(relay log)到其他的slave
4、应用从master保存的二进制日志事件(binlog events)
5、提升一个slave为新的master
6、使其他的slave连接新的master进行复制
注意: 为了尽可能的减少主库硬件损坏宕机造成的数据丢失,因此在配置MHA的同时建议配置成MySQL的半同步复制
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自定义扩展:
secondary_check_script #通过多条网络路由检测master的可用性
master_ip_ailover_script #更新Application使用的masterip
shutdown_script #强制关闭master节点
report_script #发送报告
init_conf_load_script #加载初始配置参数
master_ip_online_change_script #更新master节点ip地址
MHA配置文件:
global配置,为各application提供默认配置,默认文件路径 /etc/masterha_default.cnf
application配置:为每个主从复制集群
环境准备
四台主机
10.0.0.7 CentOS7.9 MHA管理端 mha-manager
10.0.0.17 CentOS7.9 MySQL-5.7.30(已安装) master
10.0.0.27 CentOS7.9 MySQL-5.7.30(已安装) slave1
10.0.0.37 CentOS7.9 MySQL-5.7.30(已安装) slave2
在管理节点上安装两个包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不兼容
两个安装包:
mha4mysql-manager
mha4mysql-node
#一些依赖包,所有节点全装,不然可能报错
yum -y install perl-DBD-MySQL
yum -y install perl-Config-Tiny
yum -y install perl-Log-Dispatch
yum -y install perl-Parallel-ForkManager
1. 在MHA管理节点上安装manager包和node包
[root@mha-manager ~]#ll mha4mysql-*.rpm
-rw-r--r-- 1 root root 81024 Aug 3 15:36 mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
-rw-r--r-- 1 root root 36328 Aug 3 15:38 mha4mysql-node-0.58-0.el7.centos.noarch.rpm
[root@mha-manager ~]#yum -y install mha4mysql-*.rpm
2. 在所有MySQL服务器上安装mha4mysql-node包
#此包支持CentOS 8,7,6
mha4mysql-node
[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
3. 在所有节点实现相互之间ssh key验证
[root@mha-manager ~]#ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:WYwg2mUTQaW07CGfN6+/i+mChmnfsTdd+BfunJ1NTbU root@mha-manager
The key's randomart image is:
+---[RSA 2048]----+
| ..X+. |
| o * = o |
| . o = . o .|
| + o o o|
| + S . E.|
| . o. . ...|
| o .. ..o . .o|
| + o..oo+. ..o+o|
| . o. o+=o+o o+.o|
+----[SHA256]-----+
[root@mha-manager ~]#ssh-copy-id 127.0.0.1
[root@mha-manager ~]#rsync -av .ssh 10.0.0.17:/root/
[root@mha-manager ~]#rsync -av .ssh 10.0.0.27:/root/
[root@mha-manager ~]#rsync -av .ssh 10.0.0.37:/root/
[root@mha-manager ~]#ssh 10.0.0.37
Last login: Wed Aug 3 15:27:52 2022 from 10.0.0.1
[root@slave2 ~]#ssh 10.0.0.27
Last login: Wed Aug 3 15:27:37 2022 from 10.0.0.1
[root@slave1 ~]#ssh 10.0.0.17
Last login: Wed Aug 3 15:26:35 2022 from 10.0.0.1
[root@master ~]#exit
logout
Connection to 10.0.0.17 closed.
[root@slave1 ~]#exit
logout
Connection to 10.0.0.27 closed.
[root@slave2 ~]#exit
logout
Connection to 10.0.0.37 closed.
[root@mha-manager ~]#
4. 在管理节点建立配置文件
#注意: 此文件的行尾不要加空格等符号,可以用vim的:set list或cat -A /etc/mastermha/app1.cnf查看
[root@mha-manager ~]#mkdir /etc/mastermha/
[root@mha-manager ~]#vim /etc/mastermha/app1.cnf
[server default]
user=mhauser #用于远程连接MySQL所有节点的用户,需要有管理员的权限
password=linux2022.
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=linux2022.
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/logbin/ #指定二进制日志存放的目录,mha4mysql-manager-0.58必须指定,之前版本不需要指定
[server1]
hostname=10.0.0.17
candidate_master=1
[server2]
hostname=10.0.0.27
candidate_master=1 #设置为优先候选master,即使不是集群中事件最新的slave,也会优先当master
[server3]
hostname=10.0.0.37
#最终文件内容
[root@mha-manager ~]#cat /etc/mastermha/app1.cnf
[server default]
user=mhauser
password=linux2022.
manager_workdir=/data/mastermha/app1/
manager_log=/data/mastermha/app1/manager.log
remote_workdir=/data/mastermha/app1/
ssh_user=root
repl_user=repluser
repl_password=linux2022.
ping_interval=1
master_ip_failover_script=/usr/local/bin/master_ip_failover
report_script=/usr/local/bin/sendmail.sh
check_repl_delay=0
master_binlog_dir=/data/logbin/
[server1]
hostname=10.0.0.17
candidate_master=1
[server2]
hostname=10.0.0.27
candidate_master=1
[server3]
hostname=10.0.0.37
说明: 主库宕机谁来接管新的master
1. 所有从节点日志都是一致的,默认会以配置文件的顺序去选择一个新主
2. 从节点日志不一致,自动选择最接近于主库的从库充当新主
3. 如果对于某节点设定了权重(candidate_master=1),权重节点会优先选择。但是此节点日志量落后主
库超过100M日志的话,也不会被选择。可以配合check_repl_delay=0,关闭日志量的检查,强制选择候选
节点
5. 相关脚本
#邮件配置文件:/etc/mail.rc
[root@mha-manager ~]#vim /usr/local/bin/sendmail.sh
#!/bin/bash
echo "MySQL is down" | mail -s "MHA Warning" 13530176733@163.com
[root@mha-manager ~]#chmod +x /usr/local/bin/sendmail.sh
#master_ip_failover脚本说明:
my $vip = '10.0.0.100/24'; #设置Virtual IP
my $gateway = '10.0.0.2'; #网关Gateway IP
my $interface = 'eth0'; #指定VIP所在网卡
[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 = '10.0.0.100/24';
my $gateway = '10.0.0.2';
my $interface = 'eth0';
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
6. 实现Master
[root@master ~]#vim /etc/my.cnf
server_id=17
binlog_format=row
log-bin=/data/logbin/mysql-bin
skip_name_resolve=1 #禁止反向解析
general_log #通用日志,观察结果,非必须项,生产无需启用
[root@master ~]#mkdir /data/logbin/ -p
[root@master ~]#chown -R mysql.mysql /data/logbin/
[root@master ~]#systemctl stop mysqld.service
[root@master ~]#systemctl daemon-reload
[root@master ~]#systemctl restart mysqld.service
mysql> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 154 |
+------------------+-----------+
mysql> create user repluser@'10.0.0.%' identified by 'linux2022.';
mysql> grant replication slave on *.* to repluser@'10.0.0.%';
mysql> create user mhauser@'10.0.0.%' identified by 'linux2022.';
mysql> grant all on *.* to mhauser@'10.0.0.%';
mysql> FLUSH PRIVILEGES;
mysql> exit
#配置VIP
[root@master ~]#ifconfig eth0:1 10.0.0.100/24
[root@master ~]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:50:56:3d:cb:17 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.17/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
valid_lft forever preferred_lft forever
inet6 fe80::190b:996d:c812:97b5/64 scope link noprefixroute
valid_lft forever preferred_lft forever
mysql> create database db1;
7. 实现slave1
[root@slave1 ~]#vim /etc/my.cnf
server_id=27
binlog_format=row
read_only=on
log-bin=/data/logbin/mysql-bin
relay_log_purge=0
skip_name_resolve=1
general_log
[root@slave1 ~]#mkdir /data/logbin/ -p
[root@slave1 ~]#chown -R mysql.mysql /data/logbin/
[root@slave1 ~]#systemctl stop mysqld.service
[root@slave1 ~]#systemctl daemon-reload
[root@slave1 ~]#systemctl restart mysqld.service
mysql> CHANGE MASTER TO
-> MASTER_HOST='10.0.0.17',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='linux2022.',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=154;
mysql> start slave;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.17
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1252
Relay_Log_File: slave1-relay-bin.000002
Relay_Log_Pos: 1418
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1252
Relay_Log_Space: 1626
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 17
Master_UUID: fa431e17-12dd-11ed-a6a9-0050563dcb17
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| mysql |
| performance_schema |
| sys |
+--------------------+
mysql> show slave status\G
Empty set (0.07 sec)
mysql> select @@read_only;
+-------------+
| @@read_only |
+-------------+
| 0 |
+-------------+
8. 实现slave2
[root@slave2 ~]#vim /etc/my.cnf
server_id=37
binlog_format=row
read_only=on
log-bin=/data/logbin/mysql-bin
relay_log_purge=0
skip_name_resolve=1
general_log
[root@slave2 ~]#mkdir /data/logbin/ -p
[root@slave2 ~]#chown -R mysql.mysql /data/logbin/
[root@slave2 ~]#systemctl stop mysqld.service
[root@slave2 ~]#systemctl daemon-reload
[root@slave2 ~]#systemctl restart mysqld.service
mysql> CHANGE MASTER TO
-> MASTER_HOST='10.0.0.17',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='linux2022.',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=154;
mysql> start slave;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.17
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1252
Relay_Log_File: slave2-relay-bin.000002
Relay_Log_Pos: 1418
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1252
Relay_Log_Space: 1626
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 17
Master_UUID: fa431e17-12dd-11ed-a6a9-0050563dcb17
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| mysql |
| performance_schema |
| sys |
+--------------------+
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.27 #10.0.0.27为新的master
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 154
Relay_Log_File: slave2-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 528
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 27
Master_UUID: fe2d2cc8-12f1-11ed-aac8-0050563b17bf
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.01 sec)
9. 检查MHA的环境
#检查环境
[root@mha-manager ~]#masterha_check_ssh --conf=/etc/mastermha/app1.cnf
Sun Aug 21 19:44:13 2022 - [info] All SSH connection tests passed successfully.
[root@mha-manager ~]#masterha_check_repl --conf=/etc/mastermha/app1.cnf
MySQL Replication Health is OK.
#查看状态
[root@mha-manager ~]#masterha_check_status --conf=/etc/mastermha/app1.cnf
app1 is stopped(2:NOT_RUNNING).
10. 启动MHA
#开启MHA,默认是前台运行,生产环境一般为后台执行
[root@mha-manager ~]#nohup masterha_manager --conf=/etc/mastermha/app1.cnf &> /dev/null
[root@mha-manager ~]#masterha_manager --conf=/etc/mastermha/app1.cnf
Sun Aug 21 23:37:51 2022 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sun Aug 21 23:37:51 2022 - [info] Reading application default configuration from /etc/mastermha/app1.cnf..
Sun Aug 21 23:37:51 2022 - [info] Reading server configuration from /etc/mastermha/app1.cnf..
#查看到健康性检查
[root@master ~]#tail -f /data/mysql/master.log
2022-08-21T15:44:28.793988Z 12 Query SELECT 1 As Value
2022-08-21T15:44:29.807376Z 12 Query SELECT 1 As Value
2022-08-21T15:44:30.845362Z 12 Query SELECT 1 As Value
2022-08-21T15:44:31.804686Z 12 Query SELECT 1 As Value
2022-08-21T15:44:32.811690Z 12 Query SELECT 1 As Value
2022-08-21T15:44:33.810738Z 12 Query SELECT 1 As Value
2022-08-21T15:44:34.814359Z 12 Query SELECT 1 As Value
#查看状态
[root@mha-manager ~]#masterha_check_status --conf=/etc/mastermha/app1.cnf
app1 (pid:2567) is running(0:PING_OK), master:10.0.0.17
## 11. 排错日志
11. 排错日志
[root@mha-manager ~]#tail -f /data/mastermha/app1/manager.log
Sun Aug 21 23:51:16 2022 - [info] Starting ping health check on 10.0.0.17(10.0.0.17:3306)..
Sun Aug 21 23:51:16 2022 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..
12. 模拟故障
#当master down机后,mha管理程序自动退出
[root@master ~]#systemctl stop mysqld.service
[root@mha-manager ~]#masterha_manager --conf=/etc/mastermha/app1.cnf
Mon Aug 22 00:25:08 2022 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Aug 22 00:25:08 2022 - [info] Reading application default configuration from /etc/mastermha/app1.cnf..
Mon Aug 22 00:25:08 2022 - [info] Reading server configuration from /etc/mastermha/app1.cnf..
[root@mha-manager ~]#tail -f /data/mastermha/app1/manager.log
app1: MySQL Master failover 10.0.0.17(10.0.0.17:3306) to 10.0.0.27(10.0.0.27:3306) succeeded
Master 10.0.0.17(10.0.0.17:3306) is down!
Check MHA Manager logs at mha-manager:/data/mastermha/app1/manager.log for details.
Started automated(non-interactive) failover.
Invalidated master IP address on 10.0.0.17(10.0.0.17:3306)
The latest slave 10.0.0.27(10.0.0.27:3306) has all relay logs for recovery.
Selected 10.0.0.27(10.0.0.27:3306) as a new master.
10.0.0.27(10.0.0.27:3306): OK: Applying all logs succeeded.
10.0.0.27(10.0.0.27:3306): OK: Activated master IP address.
10.0.0.37(10.0.0.37:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
10.0.0.37(10.0.0.37:3306): OK: Applying all logs succeeded. Slave started, replicating from 10.0.0.27(10.0.0.27:3306)
10.0.0.27(10.0.0.27:3306): Resetting slave info succeeded.
Master failover to 10.0.0.27(10.0.0.27:3306) completed successfully.
Mon Aug 22 00:25:14 2022 - [info] Sending mail..
[root@mha-manager ~]#masterha_check_status --conf=/etc/mastermha/app1.cnf
app1 is stopped(2:NOT_RUNNING).
#验证VIP漂移至新的Master上
[root@slave1 ~]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:50:56:3b:17:bf brd ff:ff:ff:ff:ff:ff
inet 10.0.0.27/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
valid_lft forever preferred_lft forever
inet6 fe80::edd2:875b:8e59:251d/64 scope link noprefixroute
valid_lft forever preferred_lft forever
13. 收到报警邮件
14. 如果再次运行MHA,需要先删除下面文件
[root@mha-manager ~]#ll /data/mastermha/app1/app1.failover.complete
-rw-r--r-- 1 root root 0 Aug 22 00:25 /data/mastermha/app1/app1.failover.complete
[root@mha-manager ~]#rm -f /data/mastermha/app1/app1.failover.complete