小白到运维工程师自学之路 第四十四集 (mariadb高可用集群故障转移)

一、概述

        故障转移是指在集群中某个节点发生故障时,自动将服务转移到其他正常节点上的

过程。在MariaDB高可用集群中,通常使用主从复制的方式来实现故障转移。其中一个

节点被指定为主节点,负责处理所有的写操作和部分读操作,其他节点作为从节点,负

责复制主节点的数据并处理读操作。当主节点发生故障时,集群中的其他节点会自动选

举一个新的主节点,接管服务。

二、步骤流程

1. 检测故障:集群中的监控组件会定期检测各个节点的状态,一旦发现某个节点不可用,就会触发

故障转移的流程。

2. 选举新的主节点:当主节点发生故障时,集群中的其他节点会通过选举算法选择一个新的主节

点。选举算法通常基于节点的健康状态和性能指标,确保选出的节点能够正常提供服务。

3. 数据同步:新的主节点被选出后,从节点会与新的主节点进行数据同步,确保数据的一致性。这

通常通过主从复制的方式实现,新的主节点将自己的数据变更同步给从节点。

4. 客户端重连:一旦故障转移完成,客户端需要重新连接到新的主节点,以继续进行数据库操作。

三、准备工作

1、mariadb完成主从复制 (主从复制有问题的话可以去看我之前的文章哦)

2、关闭防火墙

3、mariadb服务器上安装mha4mysql-node

4、5台服务器完成ssh免密登录

5、manager节点安装mha4mysql-manager

四、部署

1、四台mariadb创建mha监控用户

grant all privileges on *.* to 'root'@'192.168.77.%' identified  by '123456'

2、为自己的主机授权

grant all privileges on *.* to 'root'@'syh1' identified  by '123456';
grant all privileges on *.* to 'root'@'syh2' identified  by '123456';
grant all privileges on *.* to 'root'@'syh3' identified  by '123456';
grant all privileges on *.* to 'root'@'syh4' identified  by '123456';

3、更新mariadb权限

flush privileges;

4、部署manager管理节点

mkdir /etc/masterha  创建配置目录复制模板文件
cp mha4mysql-manager-0.56/samples/conf/app1.cnf /etc/masterha 复制manager的配置文件到etc下
修改配置文件
[server default]
manager_workdir=/var/log/masterha/app1    #设置manager的工作日志
manager_log=/var/log/masterha/app1/manager.log  #设置manager的日志,这两条都是默认存在的
master_binlog_dir=/var/lib/mysql  #设置master默认保存binlog的位置,以便MHA可以找到master日志
master_ip_failover_script= /usr/local/bin/master_ip_failover #设置自动failover时候的切换脚本
password=123456   #设置mysql中root用户的密码
user=root
ping_interval=1   #ping包的时间间隔
remote_workdir=/tmp  #设置远端mysql在发生切换时保存binlog的具体位置
repl_password=123456  #设置复制用户的密码和用户名(这个是主从复制用的那个用户)
repl_user=repl

[server1]
hostname=syh1          提供服务的主机
port=3306              监听的端口号

[server2]
hostname=syh2
candidate_master=1     声明这个服务器是主的候选
port=3306
check_repl_delay=0      

[server3]
hostname=syh3
port=3306

[server4]
hostname=syh4
port=3306

5、编辑故障转移脚本

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 = '192.168.77.100';          	这个是虚拟IP 只要是本网段的就可以
my $key = "1"; 	
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down"; 
$ssh_user = "root"; 
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; 
#}; 
eval { 
print "Disabling the VIP on old master: $orig_master_host \n"; 
#my $ping=`ping -c 1 10.0.0.13 | grep "packet loss" | awk -F',' '{print $3}' | awk '{print $1}'`; 
#if ( $ping le "90.0%"&& $ping gt "0.0%" ){ 
#$exit_code = 0; 
#} 
#else { 
&stop_vip(); 
# updating global catalog, etc 
$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_ip \" $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"; }

五、检查mha的状态

1、检查MHA ssh通信状态(返回 successfully表示没有问题)

masterha_check_ssh --conf=/etc/masterha/app1.cnf

 2、检查整个集群的状态(返回IS OK表示没有问题)

 masterha_check_repl --conf=/etc/masterha/app1.cnf

3、检查manager状态

如果正常会显示"PING_OK"

NOT_RUNNING",代表MHA监控没有开启

masterha_check_status --conf=/etc/masterha/app1.cnf  检查命令



开启命令
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 &

 六、测试

首先停掉主服务器的状态

查看配置文件 (可以看到server1的配置文件已经没有了 server2变成了master)

 再去mariadb中查看从的状态(可以看到主服务器的IP已经变成了113)

查看现在这个主的IP(可以看到他的ip地址中多了一个100的IP 这个就是我们在脚本中定义的VIP ip)

以上就是mha高可用集群的故障转移的操作

如有问题欢迎各位大佬批评指正,我们共同进步

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值