MHA高可用配置与故障切换

2024-03-31 04:28
文章标签 配置 切换 可用 故障 mha

本文主要是介绍MHA高可用配置与故障切换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言: 

MHA高可用故障就是单点故障,那么我们如何解决单点故障MHA中Master如何将故障的机器停止,使用备用的Slave服务器

一 MHA定义

MHA(MasterHigh Availablity)是一套优秀的Mysql高可用环境下故障切换和主从复制的软件。

解决MySQL 单点的问题。

MySQL故障切换过程中,MHA能做到0-30秒内自动完成故障切换操作。

故障切换的过程中最大程度上保证数据的一致性,以达到真正意义上的高可用。

MHA是建在主从复制的基础上的;0-30秒自动完成故障切换是MHA的特性 

1 MHA组成

MHA Manager(管理节点)

MHA Node(数据节点)

MHA Manager 可以单独部署在一台独立的机器上,管理多个 master-slave 集群;也可以部署在一台 slave 节点上。
MHA Manager 会定时探测集群中的 master 节点。当 master 出现故障时,它可以自动将最新数据的 slave 提升为新的 mas然后将所有其他的 slave 重新指向新的 master。整个故障转移过程对应用程序完全透明。

Node节点就是探测Mysql数据库是否正常

2 MHA特点

自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据不丢失
使用半同步复制,可以大大降低数据丢失的风险,如果只有一个slave已经收到了最新的二进制日志,MHA可以将最新的二进制日志应用于其他所有的slave服务器上,因此可以保证所有节点的数据一致性
目前MHA支持一主多从架构,最少三台服务,即一主两从

MHA Manger 管理多组主从复制;从服务器要等主服务器宕掉后才能升为主服务器;从服务器中数

据最新最全的才会成为新的主服务器。

3 MHA工作原理(1)

从宕机崩溃的master 保存二进制日志事件(binlog  events);
识别含有最新的更新 slave 日志(可以理解为主从复制数据最新的从服务器)
应用差异的中继日志(relay log)到其他的slave(将数据信息最全最新的从服务器的中继日志给其他的从服务器)
应用从master保存的二进制日志事件
提升一个 salve 为新的master
使其他的slave连接行的master 进行复制。

4 MHA的工作原理(2)

实际就是manager的工作过程

manager会周期性探测master的状态,一旦发现master故障以后,会根据默认,将目前收到最新数

据的从节点指定为新的master,所有的其他从节点指向新的master,同时VIP从原来的主漂移到新

的主。这些对客户端来说都是透明的

有哪些数据库集群高可用方案
有keepalived(单主)、MHA(单主)、MMM(单主)以及MySQL cluster(多主)

所有的高可用都是在主从复制的基础上进行。

keepalived可以完成主备切换,但是不能完成读写分离

5 MMM的工作模式:

支持双主故障切换和双主日常管理,MMM也是用perl语言开发,主要用来监控和管理MySQL的双主复制。虽然叫做双主复制,但是业务上同一时刻只允许对一个主进行写入,另一台备选主上提供部分读服务,以加速在主主切换时备选主的预热,可以说MMM这套脚本程序一方面实现了故障切换的功能,另一方面其内部附加的工具脚本也可以实现多个 Slave 的 read 负载均衡。

二 实验:搭建 MySQL MHA

目的:解决故障切换、尽可能的保存数据,以及所有节点日志的一致性

实验思路:搭建MHA机构---->数据库安装---->一主两从---->MHA搭建

故障模拟:主服务器失效---->主服务器的备胎服务器成为主服务器---->原故障

master:192.168.11.8

slave1:192.168.11.7

slave2:192.168.11.11

MHA manager 节点服务器:192.168.11.14

1 四台服务器同时关闭防火墙防护

systemctl stop firewalldsetenforce 0

2 修改 Master、Slave1、Slave2 节点的 Mysql主配置文件/etc/my.cnf 

三台服务器都做此操作

 

 

 

3 在 Master、Slave1、Slave2 节点上都创建两个软链接

ln -s /usr/local/mysql/bin/mysql  /usr/sbin/ln -s /usr/local/mysql/bin/mysqlbinlog  /usr/sbin/

4 配置 mysql 一主两从

①所有数据库节点进行 mysql 授权
mysql -uroot -p123
grant replication slave on *.* to 'myslave'@'192.168.11.%' identified by '123';		
grant all privileges on *.* to 'mha'@'192.168.11.%' identified by 'manager';
grant all privileges on *.* to 'mha'@'master' identified by 'manager';
grant all privileges on *.* to 'mha'@'slave1' identified by 'manager';
grant all privileges on *.* to 'mha'@'slave2' identified by 'manager';
flush privileges;

 ②在 Master 节点查看二进制文件和同步点

③在 Slave1、Slave2 节点执行同步操作
change master to master_host='192.168.11.8',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=1745; start slave;
④在 Slave1、Slave2 节点查看数据同步结果 
show slave status\G		

 

⑤ 两个从库必须设置为只读模式:
set global read_only=1;

 

⑥在 Master 主库插入条数据,测试是否同步

在主服务器配置

mysql> create database kgc;
Query OK, 1 row affected (0.01 sec)mysql> use kgc;
Database changed
mysql> create table ky35(id int,name varchar(9));
Query OK, 0 rows affected (0.02 sec)mysql> insert into ky35 values(1,'heze');
Query OK, 1 row affected (0.01 sec)

两台从服务器: 

mysql> use kgc;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> select * from ky35;
+------+------+
| id   | name |
+------+------+
|    1 | heze |
+------+------+
1 row in set (0.01 sec)

5 安装MHA所有组件

①所有服务器上都安装 MHA 依赖的环境,首先安装 epel 源

四台机器同时epel源

yum install epel-release --nogpgcheck -y

四台服务器同时安装环境 

yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN

 

②安装 MHA 软件包,先在所有服务器上必须先安装 node 组件

对于每个操作系统版本不一样,这里 CentOS7.6选择 0.57 版本。
在所有服务器上必须先安装 node 组件,最后在 MHA-manager 节点上安装 manager 组件,因为 manager 依赖 node 组件。

192.168.11.14 manager
192.168.11.8 master
192.168.11.7 slave2
192.168.11.11 slave3

tar zxvf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make && make install

 

 

save_binary_logs 保存和复制 master 的二进制日志apply_diff_relay_logs 识别差异的中继日志事件并将其差异的事件应用于其他的 slavefilter_mysqlbinlog 去除不必要的 ROLLBACK 事件(MHA 已不再使用这个工具)purge_relay_logs 清除中继日志(不会阻塞 SQL 线程)
③在 MHA manager 节点上安装 manager 组件
cd /opt
tar zxvf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make && make install

6 在所有服务器上配置无密码认证

①Manager管理节点免密配置给Master、Slave1和Slave2

ssh-keygen -t rsa 				#一路按回车键ssh-copy-id 192.168.11.11ssh-copy-id 192.168.11.7ssh-copy-id 192.168.11.8
[root@mcb-11-14 .ssh]# ssh-copy-id 192.168.11.11
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.11.11 (192.168.11.11)' can't be established.
ECDSA key fingerprint is SHA256:O0XjKR3Ec+XTMGJ1HQo6AZq1kqcd8uhO9w53SHlw5As.
ECDSA key fingerprint is MD5:12:10:d5:e0:f6:7d:5c:9f:d8:59:97:cb:85:11:24:b5.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.11.11's password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh '192.168.11.11'"
and check to make sure that only the key(s) you wanted were added.[root@mcb-11-14 .ssh]# ssh-copy-id 192.168.11.7
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.11.7 (192.168.11.7)' can't be established.
ECDSA key fingerprint is SHA256:uQfWnfl20Yj/iVllTVL3GAe3b5oPUj7IkhfWji2tF4Y.
ECDSA key fingerprint is MD5:23:93:1c:28:77:cc:64:8c:b6:fb:4a:c2:90:9c:b5:1a.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.11.7's password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh '192.168.11.7'"
and check to make sure that only the key(s) you wanted were added.[root@mcb-11-14 .ssh]# ssh-copy-id 192.168.11.8
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.11.8 (192.168.11.8)' can't be established.
ECDSA key fingerprint is SHA256:qzO3TB5OcyBmfFhIFfeo2jxnv4c5AIXjwXVOjt4ws/0.
ECDSA key fingerprint is MD5:ee:b0:75:21:4d:0a:13:a4:90:2c:df:1f:1f:fa:85:6b.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.11.8's password: 

②Master主服务器免密配置给Slave1和Slave2

[root@master mha4mysql-node-0.57]#ssh-copy-id 192.168.11.7
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.11.7 (192.168.11.7)' can't be established.
ECDSA key fingerprint is SHA256:uQfWnfl20Yj/iVllTVL3GAe3b5oPUj7IkhfWji2tF4Y.
ECDSA key fingerprint is MD5:23:93:1c:28:77:cc:64:8c:b6:fb:4a:c2:90:9c:b5:1a.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.11.7's password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh '192.168.11.7'"
and check to make sure that only the key(s) you wanted were added.[root@master mha4mysql-node-0.57]#ssh-copy-id 192.168.11.11
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.11.11 (192.168.11.11)' can't be established.
ECDSA key fingerprint is SHA256:O0XjKR3Ec+XTMGJ1HQo6AZq1kqcd8uhO9w53SHlw5As.
ECDSA key fingerprint is MD5:12:10:d5:e0:f6:7d:5c:9f:d8:59:97:cb:85:11:24:b5.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.11.11's password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh '192.168.11.11'"
and check to make sure that only the key(s) you wanted were added.

③Slave2从服务器免密配置给Master和Slave1


[root@slave1 mha4mysql-node-0.57]# ssh-copy-id 192.168.11.8
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.11.8 (192.168.11.8)' can't be established.
ECDSA key fingerprint is SHA256:qzO3TB5OcyBmfFhIFfeo2jxnv4c5AIXjwXVOjt4ws/0.
ECDSA key fingerprint is MD5:ee:b0:75:21:4d:0a:13:a4:90:2c:df:1f:1f:fa:85:6b.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.11.8's password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh '192.168.11.8'"
and check to make sure that only the key(s) you wanted were added.[root@slave1 mha4mysql-node-0.57]# ssh-copy-id 192.168.11.14
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.11.14 (192.168.11.14)' can't be established.
ECDSA key fingerprint is SHA256:JAQ3v9JIlkv3lauqQxhRmSga7GPl5zIOv0THdDWT1TU.
ECDSA key fingerprint is MD5:d3:64:b1:26:6c:a5:f3:50:38:b2:db:ab:07:67:fe:00.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.11.14's password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh '192.168.11.14'"
and check to make sure that only the key(s) you wanted were added.
④Slave2从服务器免密配置给Master和Slave1

[root@slave2 ~]# ssh-copy-id 192.168.11.7
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.11.7 (192.168.11.7)' can't be established.
ECDSA key fingerprint is SHA256:uQfWnfl20Yj/iVllTVL3GAe3b5oPUj7IkhfWji2tF4Y.
ECDSA key fingerprint is MD5:23:93:1c:28:77:cc:64:8c:b6:fb:4a:c2:90:9c:b5:1a.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.11.7's password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh '192.168.11.7'"
and check to make sure that only the key(s) you wanted were added.[root@slave2 ~]# ssh-copy-id 192.168.11.8
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.11.8 (192.168.11.8)' can't be established.
ECDSA key fingerprint is SHA256:qzO3TB5OcyBmfFhIFfeo2jxnv4c5AIXjwXVOjt4ws/0.
ECDSA key fingerprint is MD5:ee:b0:75:21:4d:0a:13:a4:90:2c:df:1f:1f:fa:85:6b.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.11.8's password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh '192.168.11.8'"
and check to make sure that only the key(s) you wanted were added.[root@slave2 ~]# ssh-copy-id 192.168.11.14
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.11.14 (192.168.11.14)' can't be established.
ECDSA key fingerprint is SHA256:JAQ3v9JIlkv3lauqQxhRmSga7GPl5zIOv0THdDWT1TU.
ECDSA key fingerprint is MD5:d3:64:b1:26:6c:a5:f3:50:38:b2:db:ab:07:67:fe:00.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.11.14's password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh '192.168.11.14'"
and check to make sure that only the key(s) you wanted were added.

⑤验证免密登录

用一个服务器去登录任意一个服务器,只要能登录就行

ssh 192.168.11.11ssh 192.168.11.8ssh 192.168.11.7ssh 192.168.11.14

7 在 manager 节点上配置 MHA

①在 manager 节点上复制相关脚本到/usr/local/bin 目录

cp -rp /opt/mha4mysql-manager-0.57/samples/scripts /usr/local/binls /usr/local/bin/scriptsll /usr/local/bin/scripts/

②切换 VIP 管理的脚本到 /usr/local/bin 目录,使用master_ip_failover脚本来管理 VIP 和故障切换 
cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin 

vim /usr/local/bin/master_ip_failoverEsc      :set paste
#进入末行模式   输入set paste  不加#号键
#!/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.11.200';									#指定vip的地址(要与自己一致)
my $brdc = '192.168.11.255';								#指定vip的广播地址
my $ifdev = 'ens33';										#指定vip绑定的网卡
my $key = '1';												#指定vip绑定的虚拟网卡序列号
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";		#代表此变量值为ifconfig ens33:1 192.168.10.200
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";		#代表此变量值为ifconfig ens33:1 192.168.10.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 /etc/masterha
cp /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterhavim /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=123456
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.10.14 -s 192.168.10.15
shutdown_script=""
ssh_user=root
user=mha[server1]
hostname=192.168.11.7   
port=3306[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.11.8
port=3306[server3]
hostname=192.168.11.11
port=3306
④在 Master 节点上手动开启虚拟IP
/sbin/ifconfig ens33:1 192.168.11.200/24

一主两从全部注释掉:#default-character-set=utf8
vim /etc/my.cnf检测一下:
sed -n 3p /etc/my.cnf
#default-character-set=utf8#这里主从服务器都需要修改字符集 否则检查repl会报错systemctl restart mysqld.service 
④在 manager 节点上测试 ssh 无密码认证,如果正常最后会输出 successfully,
masterha_check_ssh -conf=/etc/masterha/app1.cnf
[root@mcb-11-14 mha4mysql-manager-0.57]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
Sat Mar 30 11:37:01 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sat Mar 30 11:37:01 2024 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Sat Mar 30 11:37:01 2024 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Sat Mar 30 11:37:01 2024 - [info] Starting SSH connection tests..
Sat Mar 30 11:37:03 2024 - [debug] 
Sat Mar 30 11:37:01 2024 - [debug]  Connecting via SSH from root@192.168.11.7(192.168.11.7:22) to root@192.168.11.8(192.168.11.8:22)..
Sat Mar 30 11:37:02 2024 - [debug]   ok.
Sat Mar 30 11:37:02 2024 - [debug]  Connecting via SSH from root@192.168.11.7(192.168.11.7:22) to root@192.168.11.11(192.168.11.11:22)..
Sat Mar 30 11:37:03 2024 - [debug]   ok.
Sat Mar 30 11:37:03 2024 - [debug] 
Sat Mar 30 11:37:01 2024 - [debug]  Connecting via SSH from root@192.168.11.8(192.168.11.8:22) to root@192.168.11.7(192.168.11.7:22)..
Sat Mar 30 11:37:02 2024 - [debug]   ok.
Sat Mar 30 11:37:02 2024 - [debug]  Connecting via SSH from root@192.168.11.8(192.168.11.8:22) to root@192.168.11.11(192.168.11.11:22)..
Sat Mar 30 11:37:03 2024 - [debug]   ok.
Sat Mar 30 11:37:04 2024 - [debug] 
Sat Mar 30 11:37:02 2024 - [debug]  Connecting via SSH from root@192.168.11.11(192.168.11.11:22) to root@192.168.11.7(192.168.11.7:22)..
Sat Mar 30 11:37:03 2024 - [debug]   ok.
Sat Mar 30 11:37:03 2024 - [debug]  Connecting via SSH from root@192.168.11.11(192.168.11.11:22) to root@192.168.11.8(192.168.11.8:22)..
Sat Mar 30 11:37:04 2024 - [debug]   ok.
Sat Mar 30 11:37:04 2024 - [info] All SSH connection tests passed successfully.
⑤在 manager 节点上测试 mysql 主从连接情况,最后出现 MySQL Replication Health is OK 
masterha_check_repl -conf=/etc/masterha/app1.cnf
[root@mcb-11-14 mha4mysql-manager-0.57]# vim /usr/local/bin/master_ip_failover
[root@mcb-11-14 mha4mysql-manager-0.57]# masterha_check_repl -conf=/etc/masterha/app1.cnf
Sat Mar 30 13:04:02 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sat Mar 30 13:04:02 2024 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Sat Mar 30 13:04:02 2024 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Sat Mar 30 13:04:02 2024 - [info] MHA::MasterMonitor version 0.57.
Sat Mar 30 13:04:03 2024 - [info] GTID failover mode = 0
Sat Mar 30 13:04:03 2024 - [info] Dead Servers:
Sat Mar 30 13:04:03 2024 - [info] Alive Servers:
Sat Mar 30 13:04:03 2024 - [info]   192.168.11.8(192.168.11.8:3306)
Sat Mar 30 13:04:03 2024 - [info]   192.168.11.7(192.168.11.7:3306)
Sat Mar 30 13:04:03 2024 - [info]   192.168.11.11(192.168.11.11:3306)
Sat Mar 30 13:04:03 2024 - [info] Alive Slaves:
Sat Mar 30 13:04:03 2024 - [info]   192.168.11.7(192.168.11.7:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Sat Mar 30 13:04:03 2024 - [info]     Replicating from 192.168.11.8(192.168.11.8:3306)
Sat Mar 30 13:04:03 2024 - [info]     Primary candidate for the new Master (candidate_master is set)
Sat Mar 30 13:04:03 2024 - [info]   192.168.11.11(192.168.11.11:3306)  Version=5.7.17 (oldest major version between slaves) log-bin:disabled
Sat Mar 30 13:04:03 2024 - [info]     Replicating from 192.168.11.8(192.168.11.8:3306)
Sat Mar 30 13:04:03 2024 - [info] Current Alive Master: 192.168.11.8(192.168.11.8:3306)
Sat Mar 30 13:04:03 2024 - [info] Checking slave configurations..
Sat Mar 30 13:04:03 2024 - [info]  read_only=1 is not set on slave 192.168.11.7(192.168.11.7:3306).
Sat Mar 30 13:04:03 2024 - [warning]  relay_log_purge=0 is not set on slave 192.168.11.7(192.168.11.7:3306).
Sat Mar 30 13:04:03 2024 - [info]  read_only=1 is not set on slave 192.168.11.11(192.168.11.11:3306).
Sat Mar 30 13:04:03 2024 - [warning]  relay_log_purge=0 is not set on slave 192.168.11.11(192.168.11.11:3306).
Sat Mar 30 13:04:03 2024 - [warning]  log-bin is not set on slave 192.168.11.11(192.168.11.11:3306). This host cannot be a master.
Sat Mar 30 13:04:03 2024 - [info] Checking replication filtering settings..
Sat Mar 30 13:04:03 2024 - [info]  binlog_do_db= , binlog_ignore_db= 
Sat Mar 30 13:04:03 2024 - [info]  Replication filtering check ok.
Sat Mar 30 13:04:03 2024 - [info] GTID (with auto-pos) is not supported
Sat Mar 30 13:04:03 2024 - [info] Starting SSH connection tests..
Sat Mar 30 13:04:06 2024 - [info] All SSH connection tests passed successfully.
Sat Mar 30 13:04:06 2024 - [info] Checking MHA Node version..
Sat Mar 30 13:04:08 2024 - [info]  Version check ok.
Sat Mar 30 13:04:08 2024 - [info] Checking SSH publickey authentication settings on the current master..
Sat Mar 30 13:04:08 2024 - [info] HealthCheck: SSH to 192.168.11.8 is reachable.
Sat Mar 30 13:04:09 2024 - [info] Master MHA Node version is 0.57.
Sat Mar 30 13:04:09 2024 - [info] Checking recovery script configurations on 192.168.11.8(192.168.11.8:3306)..
Sat Mar 30 13:04:09 2024 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/usr/local/mysql/data --output_file=/tmp/save_binary_logs_test --manager_version=0.57 --start_file=master-bin.000001 
Sat Mar 30 13:04:09 2024 - [info]   Connecting to root@192.168.11.8(192.168.11.8:22).. Creating /tmp if not exists..    ok.Checking output directory is accessible or not..ok.Binlog found at /usr/local/mysql/data, up to master-bin.000001
Sat Mar 30 13:04:09 2024 - [info] Binlog setting check done.
Sat Mar 30 13:04:09 2024 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Sat Mar 30 13:04:09 2024 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.11.7 --slave_ip=192.168.11.7 --slave_port=3306 --workdir=/tmp --target_version=5.7.17-log --manager_version=0.57 --relay_log_info=/usr/local/mysql/data/relay-log.info  --relay_dir=/usr/local/mysql/data/  --slave_pass=xxx
Sat Mar 30 13:04:09 2024 - [info]   Connecting to root@192.168.11.7(192.168.11.7:22).. Checking slave recovery environment settings..Opening /usr/local/mysql/data/relay-log.info ... ok.Relay log found at /usr/local/mysql/data, up to relay-log-bin.000006Temporary relay log file is /usr/local/mysql/data/relay-log-bin.000006Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.done.Testing mysqlbinlog output.. done.Cleaning up test file(s).. done.
Sat Mar 30 13:04:10 2024 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.11.11 --slave_ip=192.168.11.11 --slave_port=3306 --workdir=/tmp --target_version=5.7.17 --manager_version=0.57 --relay_log_info=/usr/local/mysql/data/relay-log.info  --relay_dir=/usr/local/mysql/data/  --slave_pass=xxx
Sat Mar 30 13:04:10 2024 - [info]   Connecting to root@192.168.11.11(192.168.11.11:22).. Checking slave recovery environment settings..Opening /usr/local/mysql/data/relay-log.info ... ok.Relay log found at /usr/local/mysql/data, up to relay-log-bin.000006Temporary relay log file is /usr/local/mysql/data/relay-log-bin.000006Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.done.Testing mysqlbinlog output.. done.Cleaning up test file(s).. done.
Sat Mar 30 13:04:10 2024 - [info] Slaves settings check done.
Sat Mar 30 13:04:10 2024 - [info] 
192.168.11.8(192.168.11.8:3306) (current master)+--192.168.11.7(192.168.11.7:3306)+--192.168.11.11(192.168.11.11:3306)Sat Mar 30 13:04:10 2024 - [info] Checking replication health on 192.168.11.7..
Sat Mar 30 13:04:10 2024 - [info]  ok.
Sat Mar 30 13:04:10 2024 - [info] Checking replication health on 192.168.11.11..
Sat Mar 30 13:04:10 2024 - [info]  ok.
Sat Mar 30 13:04:10 2024 - [info] Checking master_ip_failover_script status:
Sat Mar 30 13:04:10 2024 - [info]   /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.11.8 --orig_master_ip=192.168.11.8 --orig_master_port=3306 IN SCRIPT TEST====/sbin/ifconfig ens33:1 down==/sbin/ifconfig ens33:1 192.168.11.200===Checking the Status of the script.. OK 
Sat Mar 30 13:04:11 2024 - [info]  OK.
Sat Mar 30 13:04:11 2024 - [warning] shutdown_script is not defined.
Sat Mar 30 13:04:11 2024 - [info] Got exit code 0 (Not master dead).MySQL Replication Health is OK.

8 在 manager 节点上启动 MHA

[root@mcb-11-14 mha4mysql-manager-0.57]# cd ..
[root@mcb-11-14 opt]# 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] 19409
[root@mcb-11-14 opt]# ps -aux|grep manager
root      19409  0.2  0.5 297384 21756 pts/0    S    13:32   0:00 perl /usr/local/bin/masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover
root      19631  0.0  0.0 112824   984 pts/0    S+   13:35   0:00 grep --color=auto manager
[root@mcb-11-14 opt]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:19409) is running(0:PING_OK), master:192.168.11.8
[root@mcb-11-14 opt]# 

三 故障模拟

①在 manager 节点上监控观察日志记录
tail -f /var/log/masterha/app1/manager.log

②将主服务器的Mysql服务停止
systemctl stop mysqld
或
pkill -9 mysql

正常自动切换一次后,MHA 进程会退出。HMA 会自动修改 app1.cnf 文件内容,将宕机的 master 节点删除。查看 slave1 是否接管 VIP
ifconfig

③在 manager 节点上监控再观察日志
----- Failover Report -----app1: MySQL Master failover 192.168.11.8(192.168.11.8:3306) to 192.168.11.7(192.168.11.7:3306) succeededMaster 192.168.11.8(192.168.11.8:3306) is down!Check MHA Manager logs at mcb-11-14:/var/log/masterha/app1/manager.log for details.Started automated(non-interactive) failover.
Invalidated master IP address on 192.168.11.8(192.168.11.8:3306)
The latest slave 192.168.11.7(192.168.11.7:3306) has all relay logs for recovery.
Selected 192.168.11.7(192.168.11.7:3306) as a new master.
192.168.11.7(192.168.11.7:3306): OK: Applying all logs succeeded.
192.168.11.7(192.168.11.7:3306): OK: Activated master IP address.
192.168.11.11(192.168.11.11:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
192.168.11.11(192.168.11.11:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.11.7(192.168.11.7:3306)
192.168.11.7(192.168.11.7:3306): Resetting slave info succeeded.
Master failover to 192.168.11.7(192.168.11.7:3306) completed successfully.

可以去manager查看配置文件slave1去除了

vim /etc/masterha/app1.cnf

四 故障修复:

① 模拟修复原 master mysql

systemctl restart mysqld.service

②在现主库服务器 mysql2 查看二进制文件和同步点 

[root@master opt]#systemctl restart mysqld
[root@master opt]#mysql -uroot -p123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17-log Source distributionCopyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000002 |      154 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

③ 在原主库服务器 mysql1 执行同步操作


mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000002 |      154 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)mysql> change master to master_host='192.168.11.8',master_user='myslave',master_password='123',master_log_file='master-bin.000002',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.02 sec)mysql> start slave;
Query OK, 0 rows affected (0.00 sec)mysql> show slave status\G

④ 在 manager 节点上修改配置文件app1.cnf

[root@mcb-11-14 opt]# vim /etc/masterha/app1.cnf

 ⑤ 在 manager 节点上启动 MHA

⑥ 在现主库服务器 mysql2 更新数据,在现从服务器 mysql1、mysql3 查看数据

这篇关于MHA高可用配置与故障切换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/863387

相关文章

最新版IDEA配置 Tomcat的详细过程

《最新版IDEA配置Tomcat的详细过程》本文介绍如何在IDEA中配置Tomcat服务器,并创建Web项目,首先检查Tomcat是否安装完成,然后在IDEA中创建Web项目并添加Web结构,接着,... 目录配置tomcat第一步,先给项目添加Web结构查看端口号配置tomcat    先检查自己的to

React实现原生APP切换效果

《React实现原生APP切换效果》最近需要使用Hybrid的方式开发一个APP,交互和原生APP相似并且需要IM通信,本文给大家介绍了使用React实现原生APP切换效果,文中通过代码示例讲解的非常... 目录背景需求概览技术栈实现步骤根据 react-router-dom 文档配置好路由添加过渡动画使用

Servlet中配置和使用过滤器的步骤记录

《Servlet中配置和使用过滤器的步骤记录》:本文主要介绍在Servlet中配置和使用过滤器的方法,包括创建过滤器类、配置过滤器以及在Web应用中使用过滤器等步骤,文中通过代码介绍的非常详细,需... 目录创建过滤器类配置过滤器使用过滤器总结在Servlet中配置和使用过滤器主要包括创建过滤器类、配置过滤

如何测试计算机的内存是否存在问题? 判断电脑内存故障的多种方法

《如何测试计算机的内存是否存在问题?判断电脑内存故障的多种方法》内存是电脑中非常重要的组件之一,如果内存出现故障,可能会导致电脑出现各种问题,如蓝屏、死机、程序崩溃等,如何判断内存是否出现故障呢?下... 如果你的电脑是崩溃、冻结还是不稳定,那么它的内存可能有问题。要进行检查,你可以使用Windows 11

在 VSCode 中配置 C++ 开发环境的详细教程

《在VSCode中配置C++开发环境的详细教程》本文详细介绍了如何在VisualStudioCode(VSCode)中配置C++开发环境,包括安装必要的工具、配置编译器、设置调试环境等步骤,通... 目录如何在 VSCode 中配置 C++ 开发环境:详细教程1. 什么是 VSCode?2. 安装 VSCo

在Spring中配置Quartz的三种方式

《在Spring中配置Quartz的三种方式》SpringQuartz是一个任务调度框架,它允许我们定期执行特定的任务,在Spring中,我们可以通过多种方式来配置Quartz,包括使用​​@Sche... 目录介绍使用 ​​@Scheduled​​ 注解XML 配置Java 配置1. 创建Quartz配置

Spring Boot实现多数据源连接和切换的解决方案

《SpringBoot实现多数据源连接和切换的解决方案》文章介绍了在SpringBoot中实现多数据源连接和切换的几种方案,并详细描述了一个使用AbstractRoutingDataSource的实... 目录前言一、多数据源配置与切换方案二、实现步骤总结前言在 Spring Boot 中实现多数据源连接

Kibana的安装和配置全过程

《Kibana的安装和配置全过程》Kibana是一个开源的数据分析和可视化平台,它与Elasticsearch紧密集成,提供了一个直观的Web界面,使您可以快速地搜索、分析和可视化数据,在本文中,我们... 目录Kibana的安装和配置1.安装Java运行环境2.下载Kibana3.解压缩Kibana4.配

tomcat在nginx中的配置方式

《tomcat在nginx中的配置方式》文章介绍了如何在Linux系统上安装和配置Tomcat,并通过Nginx进行代理,首先,下载并解压Tomcat压缩包,然后启动Tomcat并查看日志,接着,配置... 目录一、下载安装tomcat二、启动tomcat三、配置nginx总结提示:文章写完后,目录可以自动

Nacos客户端本地缓存和故障转移方式

《Nacos客户端本地缓存和故障转移方式》Nacos客户端在从Server获得服务时,若出现故障,会通过ServiceInfoHolder和FailoverReactor进行故障转移,ServiceI... 目录1. ServiceInfoHolder本地缓存目录2. FailoverReactorinit