MySQL MHA 故障转移-VIP

MHA故障转移-VIP

#手工在主库添加VIP
ifconfig ens33:1 192.168.80.200/24

配置VIP脚本

vim /usr/local/bin/master_ip_failover

chmod +x /usr/local/bin/

#!/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.80.200/24';                                                          #指定vip的地址,自己指定
my $brdc = '192.168.80.255';                                                            #指定vip的广播地址
my $ifdev = 'ens33';                                                                            #指定vip绑定的网卡
my $key = '1';                                                                                          #指定vip绑定的虚拟网卡序列号
my $ssh_start_vip = "/sbin/ifconfig $ifdev:$key $vip";          #代表此变量值为ifconfig ens33:1 192.168.80.200
my $ssh_stop_vip = "/sbin/ifconfig $ifdev:$key down";           #代表此变量值为ifconfig ens33:1 192.168.80.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";
}

修改manager配置文件

[server default]
manager_log=/data/mha/mysql3306/manager.log
manager_workdir=/data/mha/mysql3306/manager
master_binlog_dir=/data/mha/mysql3306/
user=mha
password=mha
ping_interval=2
repl_password=123456
repl_user=repl
ssh_user=root
master_ip_failover_script=/usr/local/bin/master_ip_failover

[server1]
hostname=192.168.80.22
port=3306

[server2]
hostname=192.168.80.11
candidate_master=1
port=3306

[server3]
hostname=192.168.80.33
port=3306

重启MHA

sh start_mha.sh

masterha_check_status --conf=/data/mha/mha_3306.cnf

灾备演练

#kill 主节点mysql进程

[root@master ~]# ps -ef|grep mysql
root      52566   1775  0 03:37 pts/0    00:00:00 /bin/sh ./mysqld_safe --defaults-file=/data/mysql3306/etc/my.cnf --user=mysql
mysql     52900  52566  0 03:37 pts/0    00:00:05 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql3306/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql3306/log/mysql.err --pid-file=/data/mysql3306/data/mysql.pid --socket=/data/mysql3306/tmp/mysql.sock --port=3306
root      61368   1775  0 04:03 pts/0    00:00:00 grep --color=auto mysql
[root@master ~]# kill -9 52566 52900

查看MHA 日志

Checking the Status of the script.. OK
Mon Jun 23 04:13:47 2025 - [info]  OK.
Mon Jun 23 04:13:47 2025 - [warning] shutdown_script is not defined.
Mon Jun 23 04:13:47 2025 - [info] Set master ping interval 2 seconds.
Mon Jun 23 04:13:47 2025 - [warning] secondary_check_script is not defined. It is highly recommended setting it to check ma                              ster reachability from two or more routes.
Mon Jun 23 04:13:47 2025 - [info] Starting ping health check on 192.168.80.11(192.168.80.11:3306)..
Mon Jun 23 04:13:47 2025 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..
Mon Jun 23 04:14:35 2025 - [warning] Got error on MySQL select ping: 2006 (MySQL server has gone away)
Mon Jun 23 04:14:35 2025 - [info] Executing SSH check script: exit 0
Mon Jun 23 04:14:35 2025 - [info] HealthCheck: SSH to 192.168.80.11 is reachable.
Mon Jun 23 04:14:37 2025 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '192.168.80.11' (11                              1))
Mon Jun 23 04:14:37 2025 - [warning] Connection failed 2 time(s)..
Mon Jun 23 04:14:39 2025 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '192.168.80.11' (11                              1))
Mon Jun 23 04:14:39 2025 - [warning] Connection failed 3 time(s)..
Mon Jun 23 04:14:41 2025 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '192.168.80.11' (11                              1))
Mon Jun 23 04:14:41 2025 - [warning] Connection failed 4 time(s)..
Mon Jun 23 04:14:41 2025 - [warning] Master is not reachable from health checker!
Mon Jun 23 04:14:41 2025 - [warning] Master 192.168.80.11(192.168.80.11:3306) is not reachable!
Mon Jun 23 04:14:41 2025 - [warning] SSH is reachable.
Mon Jun 23 04:14:41 2025 - [info] Connecting to a master server failed. Reading configuration file /etc/masterha_default.cn                              f and /data/mha/mha_3306.cnf again, and trying to connect to all servers to check server status..
Mon Jun 23 04:14:41 2025 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Jun 23 04:14:41 2025 - [info] Reading application default configuration from /data/mha/mha_3306.cnf..
Mon Jun 23 04:14:41 2025 - [info] Reading server configuration from /data/mha/mha_3306.cnf..
[root@mha mysql3306]# tail -f manager.log
Check MHA Manager logs at mha:/data/mha/mysql3306/manager.log for details.

Started automated(non-interactive) failover.
Invalidated master IP address on 192.168.80.11(192.168.80.11:3306)
Selected 192.168.80.22(192.168.80.22:3306) as a new master.
192.168.80.22(192.168.80.22:3306): OK: Applying all logs succeeded.
192.168.80.22(192.168.80.22:3306): OK: Activated master IP address.
192.168.80.33(192.168.80.33:3306): OK: Slave started, replicating from 192.168.80.22(192.168.80.22:3306)
192.168.80.22(192.168.80.22:3306): Resetting slave info succeeded.
Master failover to 192.168.80.22(192.168.80.22:3306) completed successfully.

master 已经切到slave1,并且vip 跳到了salve1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值