MHA0.58 VIP故障自动转移

一、MHA VIP 功能

上传脚本并修改内容( /usr/local/bin/master_ip_failover )

该脚本在故障时。MHA切换时才会调用脚本

#!/usr/bin/env perl

#  Copyright (C) 2011 DeNA Co.,Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#  Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

## Note: This is a sample script and is not complete. Modify the script based on your environment.

use strict;
use warnings FATAL => 'all';

use Getopt::Long;
use MHA::DBHelper;

my (
  $command,        $ssh_user,         $orig_master_host,
  $orig_master_ip, $orig_master_port, $new_master_host,
  $new_master_ip,  $new_master_port,  $new_master_user,
  $new_master_password
);
my $vip = '192.168.168.100/24';
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$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,
  'new_master_user=s'     => \$new_master_user,
  'new_master_password=s' => \$new_master_password,
);

exit &main();

sub main {
  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 {

      # 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();		#网上所有的资料,这里只有 start_vip()
            &stop_vip();		#没有这一条 stop_vip(),一帮转发别人的菜B。
            $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;
    }
}


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";
}
#这一步其实没必要做,做了也不影响
yum -y install dos2unix
dos2unix /usr/local/bin/master_ip_failover 


2、manager 配置文件添加以下内容
master_ip_failover_script = /usr/local/bin/master_ip_failover

第一次配置 VIP时,需要在主库手工创建 VIP,

192.168.168.6: ifconfig ens33:1 192.168.168.100/24

测试 脚本文件是否无误

[root@localhost bin]# masterha_check_repl --conf=/etc/masterha/app1.cnf

阻塞日志如下:

Fri Jul 17 18:38:41 2020 - [info] Checking replication health on 192.168.168.5..
Fri Jul 17 18:38:41 2020 - [info]  ok.
Fri Jul 17 18:38:41 2020 - [info] Checking replication health on 192.168.168.7..
Fri Jul 17 18:38:41 2020 - [info]  ok.
Fri Jul 17 18:38:41 2020 - [info] Checking master_ip_failover_script statussh
Fri Jul 17 18:38:41 2020 - [info]   /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.168.6 --orig_master_ip=192.168.168.6 --orig_master_port=3306 
Fri Jul 17 18:38:41 2020 - [info]  OK.
Fri Jul 17 18:38:41 2020 - [warning] shutdown_script is not defined.
Fri Jul 17 18:38:41 2020 - [info] Got exit code 0 (Not master dead).

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xpeqzgNi-1595034349087)(C:\Users\86156\AppData\Roaming\Typora\typora-user-images\image-20200718075202290.png)]


3、启动 MHA

masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf

masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:21982) is running(0:PING_OK), master:192.168.168.6


4、VIP故障转移

将主库宕机,查看VIP是否飘逸到其他主机。并查看是否生产新的 主库
在这里插入图片描述

mysql>  change master to master_auto_position=0;
\Query OK, 0 rows affected (0.11 sec)

mysql> change master to 
    -> master_user='slave',
    -> master_host='192.168.168.6',
    -> master_password='123.com',
    -> master_log_file='master-bin.000001',
    -> master_log_pos=1292;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> start slave;


关键点:

最后清空自动故障转移记录,保证下一次故障转移正常运行

[root@manager bin]# rm -rf /var/log/masterha/app1.failover.complete


二、keepalived + MHA 实现VIP

舍弃 master_ip_failover 脚本。采用 keepalived 生成 VIP地址

需要注意的是,/ertc/masterha/app1.cnf 必须设置 candidate_master=1 check_repl_delay=0参数。

在这里插入图片描述

强制指定 备用从库。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值