MySQL高可用搭建方案MHA

MHA架构介绍

MHA是Master High Availability的缩写,它是目前MySQL高可用方面的一个相对成熟的解决方案,其核心是使用perl语言编写的一组脚本,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件。在MySQL故障切换过程中,MHA能做到在0~ 30秒之内自动完数据库的故障切换操作,并且能在最大程度上保证数据的一致性,以达到真正意义上的高可用。

基于MHA的架构不像MMM那样需要搭建主主复制,只需要搭建基本的主从复制架构即可。因为MHA在主库挂掉时,是在多个从库中选取出一个从库作为新的主库。MHA集群中的各节点彼此之间均需要基于ssh 互信通信,以实现远程控制及数据管理功能。

MHA提供功能

  • 可以监控Master节点是否可用
  • 当Master不可用时,能在多个Slave中选举出新的Master
  • 提供了主从切换和故障转移功能,MHA会尝试在宕机的Master上保存binlog,在最大程度上保证事务不丢失。但如果是Master所在服务器已经无法访问,或硬件层面出现了问题,则无法成功保存binlog
  • MHA可以与半同步复制结合,避免从库之间出现数据不一致的情况
  • 支持MySQL基于GTID和基于日志点的两种复制方式

MHA故障转移过程

  • 尝试使用 ssh- 从多个Slave中识别含有最新更新的Slave,将其作为备选的Master;
  • 然后基于该Slave同步差异的中继日志 relaylog 到其他的Slave上;
  • 接着同步从原Master上保存的二进制日志事件 binlog events ;
  • 将备选的Master提升为新的Master;
  • 使其他的Slave连接新的Master进行复制;
  • 在新的Master启动vip地址,保证前端请求可以发送到新的Master。

MHA架构图

**本次实验所用机器说明 **

操作系统: CentOS Stream  9

数据库版本:MySQL8.0.36

MHA版本:0.58

名称 角色 IP manger 集群管理节点 192.168.0.110 master 主库 192.168.0.104 slave1 从库 192.168.0.103 slave2 从库 192.168.0.102

本次实验的前提是建立在已经搭建好主从复制集群的前提下,MySQL的主从复制可以参考这篇文章:MySQL5.7.43 GTID主从复制配置

但是在使用MySQL8.0.36配置主从复制的时候和MySQL5.7.43有些地方不同需要注意

等主从复制集群搭建好后就可以开始按下面的步骤开始本次实验了

配置ssh免密登陆

生成密钥

1
ssh-keygen

生成密钥和公钥后就可以在/root/.ssh/下面看到,当然这是默认的路径

将密钥拷贝到其他服务器上

1
2
3
ssh-copy-id -i /root/.ssh/id_rsa root 192.168.0.104
ssh-copy-id -i /root/.ssh/id_rsa root 192.168.0.103
ssh-copy-id -i /root/.ssh/id_rsa root 192.168.0.102

 然后到集群中其他节点上进行同样的操作,由于是重复的操作这里就不演示了,最后测试能否免密登陆即可

安装MHA软件包

MHA下载地址

mha4mysql-manager-0.58.tar.gz:

wget https://github.com/yoshinorim/mha4mysql-manager/releases/download/v0.58/mha4mysql-manager-0.58.tar.gz

mha4mysql-node-0.58.tar.gz:

wget https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58.tar.gz

在manager上安装mha4mysql-manager软件包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#下载依赖包
yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN

#进入安装目录
cd mha4mysql-manager-0.58

#编译安装
perl Makefile.PL
make && make install

在所有节点上安装mha4mysql-node软件包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#下载依赖包
yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN

#进入安装目录
cd mha4mysql-node-0.58

#编译安装
perl Makefile.PL
make && make install

关于执行perl Makefile.PL报错解决方法参考文章

Can’t locate IPC/Cmd.pm in INC (you may need to install the IPC::Cmd module

https://blog.51cto.com/u_16175492/6651879

这里模块的安装可能很慢请nai’xi

配置manger节点

创建MHA的配置文件存放目录和工作目录

1
2
[root manager ~]# mkdir /etc/mha
[root manager ~]# mkdir /home/mysql_mha

创建MHA的配置文件,并添加如下内容

这个组件secondary_check_script在安装的时候路径可能在/usr/bin/下面,需要移动到/usr/local/bin/下面 ,或者在下面的配置文件中更改路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# mha用于访问数据库的账户和密码
user mha
password 密码
# 指定mha的工作目录
manager_workdir /home/mysql_mha

# mha日志文件的存放路径
manager_log /home/mysql_mha/manager.log

# 指定mha在远程节点上的工作目录
remote_workdir /home/mysql_mha

# 可以使用sshssh_user root

# 用于主从复制的MySQL用户和密码
repl_user repl
repl_password 密码
# 指定间隔多少秒检测一次
ping_interval 1

# 指定master节点存放binlog日志文件的目录
master_binlog_dir /var/lib/mysql

# 指定一个脚本,该脚本实现了在主从切换之后,将虚拟IP漂移到新的Master上
master_ip_failover_script /usr/local/bin/master_ip_failover

# 指定用于二次检查节点状态的脚本
secondary_check_script /usr/local/bin/masterha_secondary_check -s 192.168.0.104 -s 192.168.0.103 -s 192.168.0.102

# 配置集群中的节点信息
[server1]
hostname 192.168.0.104
port 3306
# 指定该节点可以参与Master选举
candidate_master 1

[server2]
hostname 192.168.0.103
port 3306
candidate_master 1

[server3]
hostname 192.168.0.102
port 3306
# 指定该节点不能参与Master选举
no_master 1

编写配置文件中所配置的**master_ip_failover**脚本,该脚本是根据MHA的官方示例修改的,MHA默认并没有提供。需要注意脚本中的几处地方需要根据实际情况进行修改,已用注释标明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env perl

use strict;
use warnings FATAL > 'all';

use Getopt::Long;

my (
$command, $orig_master_host, $orig_master_ip,$ssh_user,
$orig_master_port, $new_master_host, $new_master_ip,$new_master_port,
$orig_master_ssh_port,$new_master_ssh_port,$new_master_user,$new_master_password
);

# 这里定义的虚拟IP可以根据实际情况进行修改
my $vip '192.168.0.111/24';
my $brdc '192.168.0.255';
my $ifdev 'eth0';
my $key '1';
# 这里的网卡名称 “eth0” 需要根据你机器的网卡名称进行修改
my $ssh_start_vip sudo /sbin/ifconfig eth0:$key $vip ;
my $ssh_stop_vip sudo /sbin/ifconfig eth0:$key down ;
my $ssh_Bcast_arp sudo /sbin/arping -I bond0 -c 3 -A $vip ;

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,
'orig_master_ssh_port i' > \$orig_master_ssh_port,
'new_master_host s' > \$new_master_host,
'new_master_ip s' > \$new_master_ip,
'new_master_port i' > \$new_master_port,
'new_master_ssh_port' > \$new_master_ssh_port,
'new_master_user' > \$new_master_user,
'new_master_password' > \$new_master_password

);

exit &main();

sub main {
$ssh_user defined $ssh_user ? $ssh_user : 'root';
print \n\nIN SCRIPT TEST $ssh_user|$ssh_stop_vip $ssh_user|$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();
&start_arp();
$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 \ ;
}
sub stop_vip() {
ssh $ssh_user\ $orig_master_host \ $ssh_stop_vip \ ;
}

sub start_arp() {
ssh $ssh_user\ $new_master_host \ $ssh_Bcast_arp \ ;
}
sub usage {
print
Usage: master_ip_failover --command start|stop|stopssh|status --ssh_user user --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是无法调用的

1
[root manager ~]# chmod a x /usr/local/bin/master_ip_failover

根据配置文件中remote_workdir的配置,需在其他节点上创建MHA的远程工作目录

1
2
3
[root master ~]# mkdir /home/mysql_mha
[root slave-01 ~]# mkdir /home/mysql_mha
[root slave-02 ~]# mkdir /home/mysql_mha

在配置文件中指定了让manager使用mha这个用户来访问数据库节点,所以需要在master节点上创建mha用户

1
2
3
create user 'mha' '%' identified with mysql_native_password by '密码';
grant all privileges on *.* to 'mha' '%';
flush privileges;

完成以上所有步骤后,在manager节点上使用**masterha_check_ssh和masterha_check_repl 对配置进行检查,其中masterha_check_ssh**用于检查ssh

1
2
[root manager ~]# masterha_check_ssh --conf /etc/mha/mysql_mha.cnf
[root manager ~]# masterha_check_repl --conf /etc/mha/mysql_mha.cnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root fa0b3e01d810 bin]# masterha_check_ssh --conf /etc/mha/mysql_mha.cnf
Mon Apr 8 16:54:15 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Apr 8 16:54:15 2024 - [info] Reading application default configuration from /etc/mha/mysql_mha.cnf..
Mon Apr 8 16:54:15 2024 - [info] Reading server configuration from /etc/mha/mysql_mha.cnf..
Mon Apr 8 16:54:15 2024 - [info] Starting SSH connection tests..
Mon Apr 8 16:54:18 2024 - [debug]
Mon Apr 8 16:54:16 2024 - [debug] Connecting via SSH from root 192.168.0.103(192.168.0.103:22) to root 192.168.0.104(192.168.0.104:22)..
Mon Apr 8 16:54:17 2024 - [debug] ok.
Mon Apr 8 16:54:17 2024 - [debug] Connecting via SSH from root 192.168.0.103(192.168.0.103:22) to root 192.168.0.102(192.168.0.102:22)..
Mon Apr 8 16:54:17 2024 - [debug] ok.
Mon Apr 8 16:54:18 2024 - [debug]
Mon Apr 8 16:54:16 2024 - [debug] Connecting via SSH from root 192.168.0.102(192.168.0.102:22) to root 192.168.0.104(192.168.0.104:22)..
Mon Apr 8 16:54:17 2024 - [debug] ok.
Mon Apr 8 16:54:17 2024 - [debug] Connecting via SSH from root 192.168.0.102(192.168.0.102:22) to root 192.168.0.103(192.168.0.103:22)..
Mon Apr 8 16:54:18 2024 - [debug] ok.
Mon Apr 8 16:54:18 2024 - [debug]
Mon Apr 8 16:54:15 2024 - [debug] Connecting via SSH from root 192.168.0.104(192.168.0.104:22) to root 192.168.0.103(192.168.0.103:22)..
Mon Apr 8 16:54:17 2024 - [debug] ok.
Mon Apr 8 16:54:17 2024 - [debug] Connecting via SSH from root 192.168.0.104(192.168.0.104:22) to root 192.168.0.102(192.168.0.102:22)..
Mon Apr 8 16:54:17 2024 - [debug] ok.
Mon Apr 8 16:54:18 2024 - [info] All SSH connection tests passed successfully.
Use of uninitialized value in exit at /usr/local/bin/masterha_check_ssh line 44.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
[root fa0b3e01d810 bin]# masterha_check_repl --conf /etc/mha/mysql_mha.cnf
Mon Apr 8 16:54:38 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Apr 8 16:54:38 2024 - [info] Reading application default configuration from /etc/mha/mysql_mha.cnf..
Mon Apr 8 16:54:38 2024 - [info] Reading server configuration from /etc/mha/mysql_mha.cnf..
Mon Apr 8 16:54:38 2024 - [info] MHA::MasterMonitor version 0.58.
Mon Apr 8 16:54:40 2024 - [info] Multi-master configuration is detected. Current primary(writable) master is 192.168.0.104(192.168.0.104:3306)
Mon Apr 8 16:54:40 2024 - [info] Master configurations are as below:
Master 192.168.0.103(192.168.0.103:3306), replicating from 192.168.0.104(192.168.0.104:3306), read-only
Master 192.168.0.104(192.168.0.104:3306), replicating from 192.168.0.103(192.168.0.103:3306)

Mon Apr 8 16:54:40 2024 - [info] GTID failover mode 1
Mon Apr 8 16:54:40 2024 - [info] Dead Servers:
Mon Apr 8 16:54:40 2024 - [info] Alive Servers:
Mon Apr 8 16:54:40 2024 - [info] 192.168.0.104(192.168.0.104:3306)
Mon Apr 8 16:54:40 2024 - [info] 192.168.0.103(192.168.0.103:3306)
Mon Apr 8 16:54:40 2024 - [info] 192.168.0.102(192.168.0.102:3306)
Mon Apr 8 16:54:40 2024 - [info] Alive Slaves:
Mon Apr 8 16:54:40 2024 - [info] 192.168.0.103(192.168.0.103:3306) Version 8.0.36 (oldest major version between slaves) log-bin:enabled
Mon Apr 8 16:54:40 2024 - [info] GTID ON
Mon Apr 8 16:54:40 2024 - [info] Replicating from 192.168.0.104(192.168.0.104:3306)
Mon Apr 8 16:54:40 2024 - [info] Primary candidate for the new Master (candidate_master is set)
Mon Apr 8 16:54:40 2024 - [info] 192.168.0.102(192.168.0.102:3306) Version 8.0.36 (oldest major version between slaves) log-bin:enabled
Mon Apr 8 16:54:40 2024 - [info] GTID ON
Mon Apr 8 16:54:40 2024 - [info] Replicating from 192.168.0.104(192.168.0.104:3306)
Mon Apr 8 16:54:40 2024 - [info] Not candidate for the new Master (no_master is set)
Mon Apr 8 16:54:40 2024 - [info] Current Alive Master: 192.168.0.104(192.168.0.104:3306)
Mon Apr 8 16:54:40 2024 - [info] Checking slave configurations..
Mon Apr 8 16:54:40 2024 - [info] Checking replication filtering settings..
Mon Apr 8 16:54:40 2024 - [info] binlog_do_db , binlog_ignore_db
Mon Apr 8 16:54:40 2024 - [info] Replication filtering check ok.
Mon Apr 8 16:54:40 2024 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Mon Apr 8 16:54:40 2024 - [info] Checking SSH publickey authentication settings on the current master..
Mon Apr 8 16:54:40 2024 - [info] HealthCheck: SSH to 192.168.0.104 is reachable.
Mon Apr 8 16:54:40 2024 - [info]
192.168.0.104(192.168.0.104:3306) (current master)
--192.168.0.103(192.168.0.103:3306)
--192.168.0.102(192.168.0.102:3306)

Mon Apr 8 16:54:40 2024 - [info] Checking replication health on 192.168.0.103..
Mon Apr 8 16:54:40 2024 - [info] ok.
Mon Apr 8 16:54:40 2024 - [info] Checking replication health on 192.168.0.102..
Mon Apr 8 16:54:40 2024 - [info] ok.
Mon Apr 8 16:54:40 2024 - [info] Checking master_ip_failover_script status:
Mon Apr 8 16:54:40 2024 - [info] /usr/local/bin/master_ip_failover --command status --ssh_user root --orig_master_host 192.168.0.104 --orig_master_ip 192.168.0.104 --orig_master_port 3306

IN SCRIPT TEST root|sudo /sbin/ifconfig eth0:1 down root|sudo /sbin/ifconfig eth0:1 192.168.0.111/24

Checking the Status of the script.. OK
Mon Apr 8 16:54:40 2024 - [info] OK.
Mon Apr 8 16:54:40 2024 - [warning] shutdown_script is not defined.
Mon Apr 8 16:54:40 2024 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

以上检测都通过后,就可以启动MHA服务了。启动命令如下

1
[root manager ~]# nohup masterha_manager --conf /etc/mha/mysql_mha.cnf &

启动完成后,可以使用ps命令查看masterha_manager进程是否存在,如下存在则代表启动成功

1
2
3
4
[root manager ~]# ps aux |grep masterha_manager
root 2842 0.3 1.1 299648 22032 pts/0 S 18:30 0:00 perl /usr/bin/masterha_manager --conf /etc/mha/mysql_mha.cnf
root 2901 0.0 0.0 112728 976 pts/0 R 18:31 0:00 grep --color auto masterha_manager
[root manager ~]#

** 最后我们需要到master节点上,手动去配置虚拟IP。因为MHA只会在主从切换时漂移虚拟IP到新的Master节点,而不会在第一次启动时主动去设置Master的虚拟IP,所以我们需要手动设置。设置虚拟IP的命令如下**

1
[root master ~]# ifconfig eth0:1 192.168.0.111/24

到此为止,我们就已经完成了MHA高可用架构的搭建,接下来我们对其进行一些简单的测试。例如,测试下是否能正常ping 通虚拟IP,毕竟应用端访问数据库时连接的是虚拟IP,所以首先得确保虚拟IP是能够被访问的。如下

MHA架构优缺点

优点:

  • 使用Perl脚本语言开发并且完全开源,开发者可以根据自己的需求进行二次开发
  • 能够支持基于GTID和基于日志点的复制模式
  • MHA在进行故障转移时更不易产生数据丢失
  • 在一个监控节点上可以监控多个Replication集群

缺点:

  • MHA默认不提供虚拟IP功能,需要自行编写脚本或利用第三方工具来实现虚拟IP的配置
  • MHA启动后只会对Master进行监控,不会对Slave进行监控,也无法监控复制链路的情况
  • 集群环境需要能够通过 ssh免密- MHA没有提供对Slave的读[负载均衡](https://cloud.tencent.com/product/clb?from_column 20065&from 20065)功能,需要通过第三方工具来实现