MySQL高可用方案 MHA
什么是 MHA
MHA(MasterHigh Availability)是一种经典的高可用架构,专门用于在主从复制环境中实现自动故障切换和最小化数据丢失。
MHA 作为 MySQL 主从复制环境下的高可用解决方案,具有自动化、低成本和稳定的优点。对于中小规模、对数据一致性要求高的单主环境,MHA 是一个非常优秀的选择。
MySQL 故障切换过程中,MHA 能做到 10-30 秒内自动完成故障切换操作。
MHA 能在故障切换的过程中最大程度上保证数据的一致性,以达到真正意义上的高可用。
MHA 的组成
MHA 由数据节点 MHA Node 和 管理节点 MHA Manager 组成。
MHA Manager(管理节点)
MHA Manager 可以单独部署在一台独立的机器上,管理多个 master-slave 集群;也可以部署在一台 slave 节点上。
MHA Manager 会定时探测集群中各个节点的状态,并在检测到 master 故障时,它可以自动将最新数据的 slave 提升为新的 master, 然后将所有其他的 slave 重新指向新的 master。整个故障转移过程对应用程序完全透明。
MHA Node(数据节点)
部署在每个 MySQL 服务器上,负责在故障转移过程中保存和恢复数据,以避免数据丢失。
MHA 的特点
自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据不丢失。
使用半同步复制,可以大大降低数据丢失的风险,如果只有一个slave已经收到了最新的二进制日志,MHA可以将最新的二进制日志应用于其他所有的slave服务器上,因此可以保证所有节点的数据一致性
MHA 对环境的要求较高,需要所有 MySQL 节点之间具备 SSH 免密登录,确保管理节点可以访问所有数据库服务器。
目前MHA支持一主多从架构,最少三台服务,即一主两从。
搭建 MySQL MHA
服务器 | 操作系统 | IP | 配置 |
---|---|---|---|
manager 节点服务器 | CentOS7.4(64 位) | 192.168.110.10 | MHA node 组件、MHA manager 组件 |
Master 节点服务器 | CentOS7.4(64 位) | 192.168.110.20 | 安装mysql8.0、MHA node 组件 |
Slave1 节点服务器 | CentOS7.4(64 位) | 192.168.110.30 | 安装mysql8.0、MHA node 组件 |
Slave2 节点服务器 | CentOS7.4(64 位) | 192.168.110.40 | 安装mysql8.0、MHA node 组件 |
关闭每个节点的核心防护和防火墙
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
修改 Master、Slave1、Slave2 节点的主机名
hostnamectl set-hostname manager
hostnamectl set-hostname mysql1
hostnamectl set-hostname mysql2
hostnamectl set-hostname mysql3
mysql80_39">Master、Slave1、Slave2 节点上安装 mysql8.0
上传软件包(mysql-8.0.33-el7-x86_64.tar.gz)
安装过程
tar -xf mysql-8.0.33-el7-x86_64.tar.gz
mv mysql-8.0.33-el7-x86_64 /usr/local/mysqluseradd -M -s /sbin/nologin mysql
chown -R mysql:mysql /usr/local/mysql/#编辑数据库配置(桌面安装的linux有自带的数据库mariadb,需要删除)
vim /etc/my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTIONchown mysql:mysql /etc/my.cnf#设置环境变量,申明/宣告mysql命令便于系统识别
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile#初始化数据库
cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data#设置系统识别,进行操作
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
systemctl daemon-reload
systemctl restart mysqld#查看数据库服务是否开启
ss -ntlp | grep mysql#初始化数据库密码(直接回车)
mysqladmin -u root -p password "123456"#进入数据库
mysql -u root -p123456
#创建用户并设置密码
create user 'root'@'%' identified by '123456';
grant all privileges on *.* to 'root'@'%';
修改 Master、Slave1、Slave2 节点的 Mysql主配置文件/etc/my.cnf
#Master 节点
vim /etc/my.cnf
[mysqld]
server-id = 1
log_bin = mysql-bin
binlog_format = mixed
log-slave-updates = true
relay-log = relay-binsystemctl restart mysqld#Slave1、Slave2 节点
vim /etc/my.cnf
server-id = 2 #三台服务器的 server-id 不能一样
log_bin = mysql-bin
binlog_format = mixed
log-slave-updates = true
relay-log = relay-binsystemctl restart mysqld
在 Master、Slave1、Slave2 节点上都创建两个软链接
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
mysql__131">配置 mysql 一主两从
#所有数据库节点进行创建用户 mysql 授权
mysql -uroot -p123456
create user 'myslave'@'192.168.110.%' identified with mysql_native_password by '123456';
create user 'manager'@'192.168.110.%' identified with mysql_native_password by '123456';
grant replication slave on *.* to 'myslave'@'192.168.110.%';
grant all privileges on *.* to 'manager'@'192.168.110.%';
flush privileges;#在 Master 节点查看二进制文件和同步点
show master status; #记下 File 和 Positionmysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1387 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)#在 Slave1、Slave2 节点执行同步操作
change master to master_host='192.168.110.20',master_user='myslave',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=1387; start slave;#在 Slave1、Slave2 节点查看数据同步结果
show slave status\G
#确保 IO 和 SQL 线程都是 Yes,代表同步正常
Slave_IO_Running: Yes
Slave_SQL_Running: Yes#Slave1、Slave2 节点必须设置为只读模式(只读度all privileges 用户无效 )
set global read_only=1;#插入数据测试数据库同步
#在 Master 主库插入条数据,测试是否同步
create database test_db;
use test_db;
create table test(id int);
insert into test(id) values (1);#在每个库查询 test 表
select * from test_db.test;
安装 MHA 软件
#所有服务器上都安装 MHA 依赖的环境,首先安装 epel 源
#MHA manager 节点:192.168.110.10
yum install epel-release --nogpgcheck -yyum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN#安装 MHA 软件包,先在所有服务器上必须先安装 node 组件
#在所有服务器上必须先安装 node 组件,最后在 MHA-manager 节点上安装 manager 组件,因为 manager 依赖 node 组件
cd /opt
tar xf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make && make install#在 MHA manager 节点上安装 manager 组件
cd /opt
tar xf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make && make install#manager 组件安装后在/usr/local/bin 下面会生成几个工具,主要包括以下几个:
masterha_check_repl:检查MySQL的复制状况
masterha_check_ssh:检查MHA的SSH配置状况
masterha_check_status:检测当前MHA运行状态
masterha_conf_host:添加或删除配置的server信息
masterha_manager:启动MHA
masterha_master_monitor:检测master是否宕机
masterha_master_switch:控制故障转移(自动或手动)
masterha_secondary_check:二次检查主库是否真有问题
masterha_stop:关闭MHA
#node 组件安装后也会在/usr/local/bin 下面会生成几个脚本(这些工具通常由 MHAManager 的脚本触发,无需人为操作)主要如下:
save_binary_logs:保存和复制master的二进制日志
apply_diff_relay_logs:识别差异的中继日志事件并将其差异的事件应用于其他的slave
filter_mysqlbinlog:去除不必要的ROLLBACK事件(MHA已不再使用这个工具)
purge_relay_logs:清除中继日志(不会阻塞SQL线程)
在所有服务器上配置无密码认证
#在 manager 节点上配置到所有数据库节点的无密码认证
ssh-keygen -t rsa
ssh-copy-id 192.168.110.10
ssh-copy-id 192.168.110.20
ssh-copy-id 192.168.110.30
ssh-copy-id 192.168.110.40#在 mysql1 上配置到数据库节点 mysql2 和 mysql3 的无密码认证
ssh-keygen -t rsa
ssh-copy-id 192.168.110.10
ssh-copy-id 192.168.110.20
ssh-copy-id 192.168.110.30
ssh-copy-id 192.168.110.40#在 mysql2 上配置到数据库节点 mysql1 和 mysql3 的无密码认证
ssh-keygen -t rsa
ssh-copy-id 192.168.110.10
ssh-copy-id 192.168.110.20
ssh-copy-id 192.168.110.30
ssh-copy-id 192.168.110.40#在 mysql3 上配置到数据库节点 mysql1 和 mysql2 的无密码认证
ssh-keygen -t rsa
ssh-copy-id 192.168.110.10
ssh-copy-id 192.168.110.20
ssh-copy-id 192.168.110.30
ssh-copy-id 192.168.110.40
在 manager 节点上配置 MHA
#在 manager 节点上复制相关脚本到/usr/local/bin 目录
cp -rp /opt/mha4mysql-manager-0.57/samples/scripts /usr/local/bin #拷贝后会有四个执行文件
ll /usr/local/bin/scripts/[root@manager ~]# 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 9870 5月 31 2015 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 目录,这里使用master_ip_failover脚本来管理 VIP 和故障切换
cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin#修改内容如下:(删除原有内容,直接复制并修改vip相关参数。可在拷贝前输入 :set paste 解决vim粘贴乱序问题)
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.110.200'; #指定vip的地址
my $brdc = '192.168.110.255'; #指定vip的广播地址
my $ifdev = 'ens33'; #指定vip绑定的网卡
my $key = '1'; #指定vip绑定的虚拟网卡序列号
my $ssh_start_vip = "sudo /sbin/ifconfig ens33:$key $vip"; #代表此变量值为ifconfig ens33:1 192.168.110.200
my $ssh_stop_vip = "sudo /sbin/ifconfig ens33:$key down"; #代表此变量值为ifconfig ens33:1 192.168.110.200 down
my $exit_code = 0; #指定退出状态码为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";
}#创建 MHA 软件目录并拷贝配置文件,这里使用app1.cnf配置文件来管理 mysql 节点服务器
#创建相关目录(所有节点)
mkdir -p /opt/mysql-mha/mha-node
# manager节点
mkdir -p /opt/mysql-mha/mha-manager
#编写配置文件
vim /opt/mysql-mha/mysql_mha.cnf[server default]
manager_log=/opt/mysql-mha/manager.log
manager_workdir=/opt/mysql-mha/mha-manager
remote_workdir=/opt/mysql-mha/mha-node
master_binlog_dir=/usr/local/mysql/data
ping_interval=1
master_ip_failover_script=/usr/local/bin/master_ip_failover
user=manager
password=123456
repl_user=myslave
repl_password=123456
port=3306
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.110.20 -s 192.168.110.30 -s 192.168.110.40
ssh_user=root[server1]
hostname=192.168.110.20
port=3306
ssh_user=root[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.110.30
port=3306
ssh_user=root[server3]
hostname=192.168.110.40
port=3306
ssh_user=root----------------------------------------------------------------------------------------------------------
[server default]
manager_log=/opt/mysql-mha/manager.log #指定manager日志路径
manager_workdir=/opt/mysql-mha/mha-manager #指定manager工作目录
remote_workdir=/opt/mysql-mha/mha-node #指定mha在远程节点上的工作目录
master_binlog_dir=/usr/local/mysql/data #指定master保存binlog的位置,这里的路径要与master里配置的binlog的路径一致,以便MHA能找到
ping_interval=1 #设置监控主库,发送ping包的时间间隔,默认是3秒,尝试三次没有回应的时候自动进行failover
master_ip_failover_script=/usr/local/bin/master_ip_failover #设置自动failover时候的切换脚本,也就是上面的那个脚本
user=manager #设置mha访问数据库的账号
password=manager123 #设置mha访问数据库的账号密码
repl_user=myslave #设置主从复制的用户
repl_password=myslave123 #设置主从复制的用户密码
report_script=/usr/local/bin/send_report #设置发生故障切换的时候发送邮件提醒
port=3306
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.110.30 -s 192.168.110.40 #指定用于二次检查节点状态的脚本
ssh_user=root #设置ssh的登录用户名[server1]
hostname=192.168.110.10
port=3306[server2]
hostname=192.168.110.30
port=3306
candidate_master=1
#设置为候选master,设置该参数以后,发生主从切换以后将会将此从库提升为主库,即使这个从库不是集群中最新的slavecheck_repl_delay=0
#默认情况下如果一个slave落后master 超过100M的relay logs的话,MHA将不会选择该slave作为一个新的master, 因为对于这个slave的恢复需要花费很长时间;通过设置check_repl_delay=0,MHA触发切换在选择一个新的master的时候将会忽略复制延时,这个参数对于设置了candidate_master=1的主机非常有用,因为这个候选主在切换的过程中一定是新的master[server3]
hostname=192.168.110.40
port=3306
在 manager 节点上测试 ssh 无密码认证,如果正常最后会输出 successfully
masterha_check_ssh -conf=/opt/mysql-mha/mysql_mha.cnf[root@manager mha4mysql-manager-0.57]# masterha_check_ssh -conf=/opt/mysql-mha/mysql_mha.cnf
Sun Oct 27 16:52:15 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sun Oct 27 16:52:15 2024 - [info] Reading application default configuration from /opt/mysql-mha/mysql_mha.cnf..
Sun Oct 27 16:52:15 2024 - [info] Reading server configuration from /opt/mysql-mha/mysql_mha.cnf..
Sun Oct 27 16:52:15 2024 - [info] Starting SSH connection tests..
Sun Oct 27 16:52:16 2024 - [debug]
Sun Oct 27 16:52:15 2024 - [debug] Connecting via SSH from root@192.168.110.20(192.168.110.20:22) to root@192.168.110.30(192.168.110.30:22)..
Sun Oct 27 16:52:15 2024 - [debug] ok.
Sun Oct 27 16:52:15 2024 - [debug] Connecting via SSH from root@192.168.110.20(192.168.110.20:22) to root@192.168.110.40(192.168.110.40:22)..
Sun Oct 27 16:52:16 2024 - [debug] ok.
Sun Oct 27 16:52:16 2024 - [debug]
Sun Oct 27 16:52:15 2024 - [debug] Connecting via SSH from root@192.168.110.30(192.168.110.30:22) to root@192.168.110.20(192.168.110.20:22)..
Sun Oct 27 16:52:16 2024 - [debug] ok.
Sun Oct 27 16:52:16 2024 - [debug] Connecting via SSH from root@192.168.110.30(192.168.110.30:22) to root@192.168.110.40(192.168.110.40:22)..
Sun Oct 27 16:52:16 2024 - [debug] ok.
Sun Oct 27 16:52:17 2024 - [debug]
Sun Oct 27 16:52:16 2024 - [debug] Connecting via SSH from root@192.168.110.40(192.168.110.40:22) to root@192.168.110.20(192.168.110.20:22)..
Sun Oct 27 16:52:16 2024 - [debug] ok.
Sun Oct 27 16:52:16 2024 - [debug] Connecting via SSH from root@192.168.110.40(192.168.110.40:22) to root@192.168.110.30(192.168.110.30:22)..
Sun Oct 27 16:52:17 2024 - [debug] ok.
Sun Oct 27 16:52:17 2024 - [info] All SSH connection tests passed successfully.
mysql__MySQL_Replication_Health_is_OK__466">在 manager 节点上测试 mysql 主从连接情况,最后出现 MySQL Replication Health is OK 字样说明正常。
masterha_check_repl -conf=/opt/mysql-mha/mysql_mha.cnf;[root@manager mha4mysql-manager-0.57]# masterha_check_repl -conf=/opt/mysql-mha/mysql_mha.cnf;
Sun Oct 27 16:52:52 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sun Oct 27 16:52:52 2024 - [info] Reading application default configuration from /opt/mysql-mha/mysql_mha.cnf..
Sun Oct 27 16:52:52 2024 - [info] Reading server configuration from /opt/mysql-mha/mysql_mha.cnf..
Sun Oct 27 16:52:52 2024 - [info] MHA::MasterMonitor version 0.57.
Sun Oct 27 16:52:53 2024 - [info] GTID failover mode = 0
Sun Oct 27 16:52:53 2024 - [info] Dead Servers:
Sun Oct 27 16:52:53 2024 - [info] Alive Servers:
Sun Oct 27 16:52:53 2024 - [info] 192.168.110.20(192.168.110.20:3306)
Sun Oct 27 16:52:53 2024 - [info] 192.168.110.30(192.168.110.30:3306)
Sun Oct 27 16:52:53 2024 - [info] 192.168.110.40(192.168.110.40:3306)
Sun Oct 27 16:52:53 2024 - [info] Alive Slaves:
......
Sun Oct 27 16:52:57 2024 - [info] Checking replication health on 192.168.110.30..
Sun Oct 27 16:52:57 2024 - [info] ok.
Sun Oct 27 16:52:57 2024 - [info] Checking replication health on 192.168.110.40..
Sun Oct 27 16:52:57 2024 - [info] ok.
Sun Oct 27 16:52:57 2024 - [info] Checking master_ip_failover_script status:
Sun Oct 27 16:52:57 2024 - [info] /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.110.20 --orig_master_ip=192.168.110.20 --orig_master_port=3306IN SCRIPT TEST====sudo /sbin/ifconfig ens33:1 down==sudo /sbin/ifconfig ens33:1 192.168.110.200===Checking the Status of the script.. OK
Sun Oct 27 16:52:57 2024 - [info] OK.
Sun Oct 27 16:52:57 2024 - [warning] shutdown_script is not defined.
Sun Oct 27 16:52:57 2024 - [info] Got exit code 0 (Not master dead).MySQL Replication Health is OK.
在 manager 节点上启动 MHA
nohup masterha_manager \
--conf=/opt/mysql-mha/mysql_mha.cnf \
--remove_dead_master_conf \
--ignore_last_failover > /var/log/mha_manager.log < /dev/null 2>&1 &----------------------------------------------------------------------------------------------------------
--remove_dead_master_conf:该参数代表当发生主从切换后,老的主库的 ip 将会从配置文件中移除。
--ignore_last_failover:在缺省情况下,如果 MHA 检测到连续发生宕机,且两次宕机间隔不足 8 小时的话,则不会进行 Failover, 之所以这样限制是为了避免 ping-pong 效应。该参数代表忽略上次 MHA 触发切换产生的文件,默认情况下,MHA 发生切换后会在 app1.failover.complete 日志文件中记录,下次再次切换的时候如果发现该目录下存在该文件将不允许触发切换, 除非在第一次切换后删除该文件,为了方便,这里设置为--ignore_last_failover。
----------------------------------------------------------------------------------------------------------
●使用&后台运行程序:结果会输出到终端;使用Ctrl+C发送SIGINT信号,程序免疫;关闭session发送SIGHUP信号,程序关闭。
●使用nohup运行程序:结果默认会输出到nohup.out;使用Ctrl+C发送SIGINT信号,程序关闭;关闭session发送SIGHUP信号,程序免疫。
●使用nohup和&配合来启动程序nohup ./test &:同时免疫SIGINT和SIGHUP信号。
----------------------------------------------------------------------------------------------------------[root@manager mha4mysql-manager-0.57]# nohup masterha_manager \
> --conf=/opt/mysql-mha/mysql_mha.cnf \
> --remove_dead_master_conf \
> --ignore_last_failover > /var/log/mha_manager.log < /dev/null 2>&1 &
[1] 90890
查看 MHA 状态
masterha_check_status --conf=/opt/mysql-mha/mysql_mha.cnf[root@manager ~]# masterha_check_status --conf=/opt/mysql-mha/mysql_mha.cnf
mysql_mha (pid:91715) is running(0:PING_OK), master:192.168.110.10
查看 MHA 日志
cat /opt/mysql-mha/manager.log | grep "current master"[root@manager mha4mysql-manager-0.57]# masterha_check_status --conf=/opt/mysql-mha/mysql_mha.cnf
mysql_mha (pid:90890) is running(0:PING_OK), master:192.168.110.20
第一次配置需要在 Master 节点上手动开启虚拟IP
/sbin/ifconfig ens33:1 192.168.110.200/24
mysql1__VIP__544">查看 mysql1 的 VIP 地址
[root@mysql1 ~]# ifconfig ens33:1
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.110.200 netmask 255.255.255.0 broadcast 192.168.110.255ether 00:0c:29:03:65:53 txqueuelen 1000 (Ethernet)
故障模拟
#在 manager 节点上监控观察日志记录
tail -f /opt/mysql-mha/manager.log#在 Master 节点 mysql1 上停止mysql服务
systemctl stop mysqld#在 manager 节点上监控观察日志记录
[root@manager mha4mysql-manager-0.57]# tail -f /opt/mysql-mha/manager.logIN SCRIPT TEST====sudo /sbin/ifconfig ens33:1 down==sudo /sbin/ifconfig ens33:1 192.168.110.200===Checking the Status of the script.. OK
Sun Oct 27 16:54:34 2024 - [info] OK.
Sun Oct 27 16:54:34 2024 - [warning] shutdown_script is not defined.
Sun Oct 27 16:54:34 2024 - [info] Set master ping interval 1 seconds.
Sun Oct 27 16:54:34 2024 - [info] Set secondary check script: /usr/local/bin/masterha_secondary_check -s 192.168.110.20 -s 192.168.110.30 -s 192.168.110.40
Sun Oct 27 16:54:34 2024 - [info] Starting ping health check on 192.168.110.20(192.168.110.20:3306)..
Sun Oct 27 16:54:34 2024 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..
Sun Oct 27 16:58:59 2024 - [warning] Got error on MySQL select ping: 1053 (Server shutdown in progress)
Sun Oct 27 16:58:59 2024 - [info] Executing SSH check script: save_binary_logs --command=test --start_pos=4 --binlog_dir=/usr/local/mysql/data --output_file=/opt/mysql-mha/mha-node/save_binary_logs_test --manager_version=0.57 --binlog_prefix=mysql-bin
......
mysql_mha: MySQL Master failover 192.168.110.20(192.168.110.20:3306) to 192.168.110.30(192.168.110.30:3306) succeededMaster 192.168.110.20(192.168.110.20:3306) is down!Check MHA Manager logs at manager:/opt/mysql-mha/manager.log for details.Started automated(non-interactive) failover.
Invalidated master IP address on 192.168.110.20(192.168.110.20:3306)
The latest slave 192.168.110.30(192.168.110.30:3306) has all relay logs for recovery.
Selected 192.168.110.30(192.168.110.30:3306) as a new master.
192.168.110.30(192.168.110.30:3306): OK: Applying all logs succeeded.
192.168.110.30(192.168.110.30:3306): OK: Activated master IP address.
192.168.110.40(192.168.110.40:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
192.168.110.40(192.168.110.40:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.110.30(192.168.110.30:3306)
192.168.110.30(192.168.110.30:3306): Resetting slave info succeeded.
Master failover to 192.168.110.30(192.168.110.30:3306) completed successfully.#查看 MHA 日志, manager关闭
masterha_check_status --conf=/opt/mysql-mha/mysql_mha.cnf[root@manager mha4mysql-manager-0.57]# masterha_check_status --conf=/opt/mysql-mha/mysql_mha.cnf
mysql_mha is stopped(2:NOT_RUNNING).#查看 mysql_mha.cnf ,故障机器被删除
cat /opt/mysql-mha/mysql_mha.cnf[root@manager mha4mysql-manager-0.57]# cat /opt/mysql-mha/mysql_mha.cnf [server default]
manager_log=/opt/mysql-mha/manager.log
manager_workdir=/opt/mysql-mha/mha-manager
master_binlog_dir=/usr/local/mysql/data
master_ip_failover_script=/usr/local/bin/master_ip_failover
password=123456
ping_interval=1
port=3306
remote_workdir=/opt/mysql-mha/mha-node
repl_password=123456
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.110.20 -s 192.168.110.30 -s 192.168.110.40
ssh_user=root
user=manager[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.110.30
port=3306
ssh_user=root[server3]
hostname=192.168.110.40
port=3306
ssh_user=root#在 mysql2 上查看VIP
ifconfig ens33:1[root@mysql2 mha4mysql-node-0.57]# ifconfig ens33:1
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.110.200 netmask 255.255.255.0 broadcast 192.168.110.255ether 00:0c:29:86:04:c4 txqueuelen 1000 (Ethernet)