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 查看数据

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/783034.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Linux】进程程序替换 做一个简易的shell

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 目录 文章目录 前言 进程程序替换 替换原理 先看代码和现象 替换函数 第一个execl()&#xff1a; 第二个execv()&#xff1a; 第三个execvp()&#xff1a; 第四个execvpe()&a…

编程语言|C语言——C语言操作符的详细解释

这篇文章主要详细介绍了C语言的操作符&#xff0c;文中通过示例代码介绍的非常详细&#xff0c;对大家的学习或者工作具有一定的参考学习价值&#xff0c;需要的朋友们下面随着小编来一起学习学习吧 一、基础 1.1 算数操作符 - * / % - * / 这些操作符是我们…

QT初识(1)

QT初识 桌面开发什么是QT下载QT安装好之后的工具AssisantDesignerQT Creator 创建一个简单的项目 我们今天来认识一下QT。 桌面开发 在了解QT&#xff0c;我们得了解一下桌面开发&#xff1a; 桌面开发指的是编写和构建在个人计算机或其他桌面操作系统&#xff08;如Windows、…

关系网络c++

题目&#xff1a; 代码&#xff1a; #include<bits/stdc.h>using namespace std;int n,x,y;struct node{int num;//编号 int t;//步数 node(){}node(int sum,int tt){numsum;ttt;} }; int mp[101][101];//图 bool flag[101];//标记 queue<node> q; void bfs() {q…

【Docker】Windows中打包dockerfile镜像导入到Linux

【Docker】Windows中打包dockerfile镜像导入到Linux 大家好 我是寸铁&#x1f44a; 总结了一篇【Docker】Windows中打包dockerfile镜像导入到Linux✨ 喜欢的小伙伴可以点点关注 &#x1f49d; 前言 今天遇到一个新需求&#xff0c;如何将Windows中打包好的dockerfile镜像给迁移…

Autodesk Maya 2025---智能建模与动画创新,重塑创意工作流程

Autodesk Maya 2025是一款顶尖的三维动画软件&#xff0c;广泛应用于影视广告、角色动画、电影特技等领域。新版本在功能上进行了全面升级&#xff0c;新增了对Apple芯片的支持&#xff0c;建模、绑定和角色动画等方面的功能也更加出色。 在功能特色方面&#xff0c;Maya 2025…

equals()和hashcode()的区别【大白话Java面试题】

equals()和hashcode()的区别 大白话 1.equals()&#xff1a;反应的是对象或变量具体的值&#xff0c;及两个对象包含的具体的值&#xff08;可能是对象的引用&#xff0c;也可能是值类型的值&#xff09; 2.hashcode():计算两个对象的哈希值&#xff0c;并返回哈希码&#xff…

逆向分析之antibot

现在太卷了&#xff0c;没资源&#xff0c;很难接到好活&#xff0c;今天群里看到个单子&#xff0c;分析了下能做&#xff0c;结果忙活了一小会&#xff0c;幸好问了下&#xff0c;人家同时有多个人再做&#xff0c;直接就拒绝再继续了。就这次忘了收定金了&#xff0c;所以原…

使用python实现i茅台自动预约

使用python实现i茅台自动预约[仅限于学习,不可商用] 运行: 直接运行 imtApi.py 打包:切换到imt脚本目录,执行打包命令: pyinstaller --onefile imtApi.py这个应用程序可以帮助你进行茅台自动化配置。以下是一些使用说明: 平台注册账号(可用i茅台)不用登录,你可以进行…

Linux的VirtualBox中USB设备无法选择USB3.0怎么办?

在VirtualBox中&#xff0c;如果遇到USB设备无法选择 USB 3.0 的问题&#xff0c;可以尝试按照以下步骤来解决&#xff1a; 确保VirtualBox版本支持USB 3.0&#xff1a;首先&#xff0c;你需要确认你的VirtualBox版本是否支持USB 3.0。一些较旧的版本可能不支持&#xff0c;因此…

一篇搞定AVL树+旋转【附图详解旋转思想】

&#x1f389;个人名片&#xff1a; &#x1f43c;作者简介&#xff1a;一名乐于分享在学习道路上收获的大二在校生 &#x1f648;个人主页&#x1f389;&#xff1a;GOTXX &#x1f43c;个人WeChat&#xff1a;ILXOXVJE &#x1f43c;本文由GOTXX原创&#xff0c;首发CSDN&…

【Effective Web】页面优化

页面优化 页面渲染流程 JavaScript 》 Style 》 Layout 》 Paint 》 Composite 首先js做了一些逻辑&#xff0c;触发了样式变化&#xff0c;style计算好这些变化后&#xff0c;把影响的dom元素进行重新布局&#xff08;layout&#xff09;,再画到画布中&#xff08;Paint&am…

半导体工艺技术

完整内容点击&#xff1a;【半导体工艺技术】

将jupyter notebook文件导出为pdf(简单有效)

1.打开jupyter notebook笔记&#xff1a; 2.点击file->print Preview 3.在新打开的页面右键打印 4.另存为PDF 5.保存即可 6.pdf效果 &#xff08;可能有少部分图片显示不了&#xff09; 网上也有其他方法&#xff0c;比如将其转换为.tex再转为PDF等&#xff0c;但个人觉…

ubuntu 中安装docker

1 资源地址 进入ubuntu官网下载Ubuntu23.04的版本的镜像 2 安装ubuntu 这里选择再Vmware上安装Ubuntu23.04.6 创建一个虚拟机&#xff0c;下一步下一步 注意虚拟机配置网络桥接&#xff0c;CD/DVD选择本地的镜像地址 开启此虚拟机&#xff0c;下一步下一步等待镜像安装。 3…

数据可视化-ECharts Html项目实战(8)

在之前的文章中&#xff0c;我们学习了如何设置散点图涟漪效果与仪表盘动态指针效果。想了解的朋友可以查看这篇文章。同时&#xff0c;希望我的文章能帮助到你&#xff0c;如果觉得我的文章写的不错&#xff0c;请留下你宝贵的点赞&#xff0c;谢谢 今天的文章&#xff0c;会…

【c++】类和对象(六)深入了解隐式类型转换

&#x1f525;个人主页&#xff1a;Quitecoder &#x1f525;专栏&#xff1a;c笔记仓 朋友们大家好&#xff0c;本篇文章我们来到初始化列表&#xff0c;隐式类型转换以及explicit的内容 目录 1.初始化列表1.1构造函数体赋值1.2初始化列表1.2.1隐式类型转换与复制初始化 1.3e…

python基础——文件操作【文件编码、文件的打开与关闭操作、文件读写操作】

&#x1f4dd;前言&#xff1a; 这篇文章主要讲解一下python中对于文件的基础操作&#xff1a; 1&#xff0c;文件编码 2&#xff0c;文件的打开与关闭操作 3&#xff0c;文件读写操作 &#x1f3ac;个人简介&#xff1a;努力学习ing &#x1f4cb;个人专栏&#xff1a;C语言入…

04 | Swoole 源码分析之 epoll 多路复用模块

首发原文链接&#xff1a;Swoole 源码分析之 epoll 多路复用模块 大家好&#xff0c;我是码农先森。 引言 在传统的IO模型中&#xff0c;每个IO操作都需要创建一个单独的线程或进程来处理&#xff0c;这样的操作会导致系统资源的大量消耗和管理开销。 而IO多路复用技术通过…

OceanBase OBCA 数据库认证专员考证视频

培训概述 OceanBase 认证是 OceanBase 官方推出的唯一人才能力认证体系&#xff0c;代表了阿里巴巴及蚂蚁集团官方对考生关于 OceanBase 技术能力的认可&#xff0c;旨在帮助考生更好地学习 OceanBase 数据库产品&#xff0c;早日融入 OceanBase 技术生态体系&#xff0c;通过由…