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 firewalld

setenforce 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 -A

Database 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 识别差异的中继日志事件并将其差异的事件应用于其他的 slave

filter_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.11

ssh-copy-id 192.168.11.7

ssh-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: 1

Now 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: 1

Now 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: 1

Now 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: 1

Now 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: 1

Now 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: 1

Now 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: 1

Now 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: 1

Now 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: 1

Now 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.11
 
ssh 192.168.11.8
 
ssh 192.168.11.7
 
ssh 192.168.11.14

7 在 manager 节点上配置 MHA

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

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

 ls /usr/local/bin/scripts

ll /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_failover

Esc      :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/masterha

vim /etc/masterha/app1.cnf
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/usr/local/mysql/data
master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
password=manager
ping_interval=1
remote_workdir=/tmp
repl_password=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.000006
    Temporary relay log file is /usr/local/mysql/data/relay-log-bin.000006
    Testing 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.000006
    Temporary relay log file is /usr/local/mysql/data/relay-log-bin.000006
    Testing 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) succeeded

Master 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@slave1 ~]#systemctl restart mysqld
[root@slave1 ~]#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 distribution

Copyright (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.000004 |      154 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

③ 在原主库服务器 mysql1 执行同步操作,同步现在主服务器的数据

mysql> change master to master_host='192.168.11.7',master_user='myslave',master_password='123',master_log_file='master-bin.000004',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.11.7
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000004
          Read_Master_Log_Pos: 154
               Relay_Log_File: master-relay-bin.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-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: 529
              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: 2
                  Master_UUID: dfeac402-e5cd-11ee-a48e-000c29ec3ca9
             Master_Info_File: /usr/local/mysql/data/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)

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

重新把三台mysql节点服务器这个记录添加进去,因为它检测到主节点失效时候会自动删除主节点

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

 ⑤ 在 manager 节点上启动 MHA

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 &
#######启动MHA

masterha_check_status --conf=/etc/masterha/app1.cnf
###查看MHA状态

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

五 总结+面试题

1 主从同步复制原理

2 读写分离你们使用什么方式?   amoeba 代理 mycat 代码 sql_proxy

通过amoeba代理服务器,实现只在主服务器上写,只在从服务器上读;
主数据库处理事务性查询,从数据库处理select 查询;
数据库复制被用来把事务查询导致的变更同步的集群中的从数据库

3 如何查看主从同步状态是否成功

在从服务器上内输入 show slave status\G 查看主从信息查看里面有IO线程的状态信息,还有master服务器的IP地址、端口事务开始号。
当 Slave_IO_Running和Slave_SQL_Running都是YES时 ,表示主从同步状态成功

4 如果I/O不是yes呢,你如何排查?

首先排查网络问题,使用ping 命令查看从服务器是否能与主服务器通信
再查看防火墙和核心防护是否关闭(增强功能)
接着查看从服务slave是否开启
两个从服务器的server-id 是否相同导致只能连接一台
master_log_file master_log_pos的值跟master值是否一致

5 show slave status能看到哪些信息(比较重要)

  • IO线程的状态信息
  • master服务器的IP地址、端口、事务开始的位置
  • 最近一次的错误信息和错误位置
  • 最近一次的I/O报错信息和ID
  • 最近一次的SQL报错信息和id

6 主从复制慢(延迟)会有哪些可能?怎么解决?

主服务器的负载过大,被多个睡眠或 僵尸线程占用  导致系统负载过大,从库硬件比主库差,导致复制延迟
主从复制单线程,如果主库写作并发太大,来不及传送到从库,就会到导致延迟
慢sql语句过多
网络延迟

7 mysql主从复制要注意:

若主从版本不一致,从的版本一定要高于主,保证可以向下兼容

因为若主的版本更新,低版本的从无法兼容的。
 

  • 29
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值