MHA实现MySQL的高可用性

对主节点进行监控,可实现自动故障转移至其它从节点;通过提升某一从节点为新的主节点,基于主从复制实现,还需要客户端配合实现。

目前MHA主要支持一主多从的架构,要搭建MHA,要求一个复制集群中必须最少有三台数据库服务器,一主二从,即一台充当master,一台充当备用master,另外一台充当从库

  • 1.MHA利用select 1 as value指定判断master的健康情况。
  • 2.如果master宕机,MHA就会从这个宕机的master中保存二进制日志
  • 3.找到从节点中谁的数据是最新的
  • 4.应用差异的中继日志(relay log)到其他的slave(最近的slave给其他slave同步)
  • 5.应用从master保存的二进制日志事件(binlog events)
  • 6.将这个slave提升为master,并使其他salve连接到新的master

注意:MHA不支持centos8(管理节点可以用centos7实现)

  • 由Manager工具包和Node工具包组成。
  • master节点需要安装manager和node两个工具包
  • slave节点只需要安装node工具包

  • 1.配置节点的master和slave

  • 2.配置mha的管理节点
  • 3.配置节点基于key验证
  • 4.在master上设置vip
  • 5.在master上检查配置是否正确
  • 6.开启mha
管理节点:CentOS7 10.0.0.132

master:CentOS8 10.0.0.10

slave1: CentOS8 10.0.0.13

slave2: CentOS8 10.0.0.14

master:

#配置server_id和开启二进制日志
[root@CentOS8 binlog]# cat /etc/my.cnf
[client-server]
include all files from the config directory
!includedir /etc/my.cnf.d

[mysqld]
server_id=10 #配置serverid
log_bin=/data/binlog/mysql-bin #开启二进制日志并指定存放位置和前缀
skip_name_resolve=1 # #禁止反向解析(不加的话ip过来回尝试解析为名称)
general_log #开启通用日志 默认的存放路径和前缀

创建用于复制的账号repluser和用户,建立一个单独的账号 mhauser,用来管理管理mysql的服务配置
mysql> create user repluser@'10.0.0.%' identified by 'redhat';
mysql> grant replication slave on *.* to repluser@'10.0.0.%'

mysql>create user mhauser@'10.0.0.%' identified by 'redhat';
mysql>grant all on *.* to mhauser@'10.0.0.%' ;

mysql> flush privileges;

slave1:

#开启二进制日志和配置server_id
[root@CentOS8 ~]# cat /etc/my.cnf

[client-server]
#
include all files from the config directory
#
!includedir /etc/my.cnf.d

[mysqld]
server_id=13
log_bin=/data/binlog/mysql-bin
general_log
skip_name_resolve=1

slave连接到master

mysql> change master to
> master_host="10.0.0.10",
> master_port=3306,
> master_user="repluser",
> master_ password="redhat",
> master_log_file='mysql-bin.000001',
> master_log_pos=156;

slave2:

操作同slave1
#配置yum源
[root@CentOS7 ~]# yum -y install epel-release
[root@CentOS7 ~]# yum clean all
[root@CentOS7 ~]# yum makecache

#安装manager和node
[root@CentOS7 ~]# yum -y install mha4mysql-manager-0.58-0.el7.centos.noarch.rpm

[root@CentOS7 ~]# yum -y install mha4mysql-node-0.58-0.el7.centos.noarch.rpm

在管理节点建立配置文件

[server default]
user=mhauser        #用于远程连接MySQL所有节点的用户,需要有管理员的权限

password=redhat

manager_workdir=/data/mastermha/app1/   #目录会自动生成,无需手动创建

manager_log=/data/mastermha/app1/manager.log

remote_workdir=/data/mastermha/app1/

ssh_user=root       #用于实现远程ssh基于KEY的连接,访问二进制日志

repl_user=repluser  #主从复制的用户信息

repl_password=redhat

ping_interval=1     #健康性检查的时间间隔

master_ip_failover_script=/usr/local/bin/master_ip_failover   #切换VIP的perl脚本

report_script=/usr/local/bin/sendmail.sh  #当执行报警脚本,发送邮件通知

check_repl_delay=0   #默认值为1,表示如果slave中从库落后主库relay log超过100M,主库不会选择这个从库为新的master,因为这个从库进行恢复需要很长的时间.通过设置参数check_repl_delay=0,mha触发主从切换时会忽略复制的延时,对于设置candidate_master=1的从库非常有用,这样确保这个从库一定能成为最新的master

master_binlog_dir=/data/mysql/  #指定二进制日志存放的目录,mha4mysql-manager-0.58必须指定,之前版本不需要指定

[server1]
hostname=10.0.0.10
candidate_master=1

[server2]
hostname=10.0.0.13
candidate_master=1   #设置为优先候选master,即使不是集群中事件最新的slave,也会优先当master

[server3]
hostname=10.0.0.14

说明: 主库宕机谁来接管新的master

1. 所有从节点日志都是一致的,默认会以配置文件的顺序去选择一个新主

2. 从节点日志不一致,自动选择最接近于主库的从库充当新主

3. 如果对于某节点设定了权重(candidate_master=1),权重节点会优先选择。但是此节点日志量落后主库超过100M日志的话,也不会被选择。可以配合check_repl_delay=0,关闭日志量的检查,强制选择候选节点

切换VIP的perl脚本:
只需要修改VIP和Gate网关还有指定网卡就行了。脚本里面不能出现注等内容,不然会报错。

[root@CentOS7 ~]#  vim /usr/local/bin/master_ip_failover
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
my (
$command, $ssh_user, $orig_master_host, $orig_master_ip,
$orig_master_port, $new_master_host, $new_master_ip, $new_master_port
);

my $vip = '10.0.0.100/24';
my $gateway = '10.0.0.2';
my $interface = 'eth0';
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig $interface:$key $vip;/sbin/arping -I
$interface -c 3 -s $vip $gateway >/dev/null 2>&1";
my $ssh_stop_vip = "/sbin/ifconfig $interface:$key down";
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" ) {
$orig_master_host, $orig_master_ip, $orig_master_port are passed.

If you manage master ip address at global catalog database,
invalidate orig_master_ip here.

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" ) {
all arguments are passed.

If you manage master ip address at global catalog database,
activate new_master_ip here.

You can also grant write access (create user, set read_only=0, etc) here.

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";
ssh $ssh_user\@$orig_master_host \" $ssh_start_vip \";
exit 0; }
else {
&usage();
exit 1; }}
A simple system call that enable the VIP on the new master
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"; }

给脚本添加可执行权限:

[root@centos7 ~] #chmod +x /usr/local/bin/master_ip_failover
[root@CentOS8 ~]# ifconfig eth0:1 10.0.0.100/24
[root@CentOS8 ~]# ssh-keygen
[root@CentOS8 ~]# ssh-copy-id 127.0.0.1
[root@CentOS8 ~]# rsync -av .ssh 10.0.0.13:/root/
[root@CentOS8 ~]# rsync -av .ssh 10.0.0.14:/root/
[root@CentOS8 ~]# rsync -av .ssh 10.0.0.132:/root/
#检查环境
[root@CentOS8 ~]# masterha_check_ssh --conf=/etc/mastermha/app1.cnf
[root@CentOS8 ~]# masterha_check_repl --conf=/etc/mastermha/app1.cnf

#查看状态
[root@CentOS8 ~]# masterha_check_status --conf=/etc/mastermha/app1.cnf
#开启MHA,默认是前台运行,生产环境一般为后台执行
nohup masterha_manager --conf=/etc/mastermha/app1.cnf &> /dev/null

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

排错日志

tail /data/mastermha/app1/manager.log
#停止mster服务
[root@CentOS8 ~]# systemctl stop mysqld.serverice

#当 master down机后,mha管理程序自动退出

#查看mha状态
[root@CentOS8 ~]# masterha_check_status --conf=/etc/mastermha/app1.cnf
app1 is stopped(2:NOT_RUNNING).

这个时候已经重新选了一个master主节点并且vip漂到对应的master节点上去了

如果再次运行MHA,需要先删除下面文件

[root@mha-manager ~]#ls /data/mastermha/app1/app1.failover.complete -l
-rw-r--r-- 1 root root 0 Aug 29 21:28 /data/mastermha/app1//app1.failover.complete

[root@mha-manager ~]#rm -f /data/mastermha/app1/app1.failover.complete

Original: https://www.cnblogs.com/heyongshen/p/16739864.html
Author: 背对背依靠
Title: MHA实现MySQL的高可用性

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/578564/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球