MHA VIP切换脚本

MHA能够在10~30秒内实现自动故障检测和故障转移,适用于对高可用性,数据完整性要求较高的场合。要做到无缝切换,还需要依赖于VIP漂移。VIP漂移比较常用的方式为使用keepalived或者使用脚本直接实现。脚本方式无须安装及复杂配置,相对简单。本文描述了基于脚本实现VIP切换。

对于keepalived的相关配置可以参考:CentOS 5.9下安装配置keepalived

1、当前主机环境及MHA配置  
[[email protected] ~]# more /etc/hosts
127.0.0.1    localhost.localdomain localhost
192.168.1.6  vdbsrv1  #master
192.168.1.7  vdbsrv2  #slave1
192.168.1.8  vdbsrv3  #slave2
192.168.1.12 vdbsrv4  #manager

###os环境
[[email protected] ~]# more /etc/issue
CentOS release 5.9 (Final)
Kernel \r on an \m

###mysql环境
[[email protected] ~]# mysql -e "show variables like ‘version‘"
+---------------+------------+
| Variable_name | Value      |
+---------------+------------+
| version       | 5.6.22-log |
+---------------+------------+

[[email protected] ~]# masterha_manager --version
masterha_manager version 0.56.

###MHA配置信息
[[email protected] ~]$ more /etc/masterha/app1.cnf
[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.log

user=mha
password=xxx
ssh_user=root
repl_user=repl  
repl_password=repl  
ping_interval=1
shutdown_script=""
master_ip_online_change_script=""
report_script=""
master_ip_failover_script=/tmp/master_ip_failover
 
[server1]
hostname=vdbsrv1
master_binlog_dir=/data/mysqldata

[server2]
hostname=vdbsrv2
master_binlog_dir=/data/mysqldata

[server3]
hostname=vdbsrv3
master_binlog_dir=/data/mysqldata/
#candidate_master=1

2、测试VIP切换
###测试VIP(192.168.1.13)是否被启用
[[email protected] ~]# ping 192.168.1.13
PING 192.168.1.13 (192.168.1.13) 56(84) bytes of data.
From 192.168.1.12 icmp_seq=10 Destination Host Unreachable

###为主机vdbsrv1添加VIP
[[email protected] ~]# ssh vdbsrv1 "/sbin/ifconfig eth0:0 192.168.1.13 netmask 255.255.255.0 up"

###校验VIP是否成功启用
[[email protected] ~]# ping 192.168.1.13
PING 192.168.1.13 (192.168.1.13) 56(84) bytes of data.
64 bytes from 192.168.1.13: icmp_seq=1 ttl=64 time=1.82 ms

###开启MHA
[[email protected] ~]# masterha_manager --conf=/etc/masterha/app1.cnf &

###模拟主库宕机
[[email protected] ~]# ssh vdbsrv1 "killall -r mysqld"

###查看管理节点日志,可以看到VIP已经漂移
[[email protected] ~]# grep VIP /var/log/masterha/app1/manager.log
Disabling the VIP on old master: vdbsrv1
Enabling the VIP - 192.168.1.13/24 on the new master - vdbsrv2

###验证VIP是否位于节点vdbsrv2
[[email protected] ~]# ssh vdbsrv2 "ifconfig |grep 1.13 -B1"
eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:5F:B2:EB 
          inet addr:192.168.1.13  Bcast:192.168.1.255  Mask:255.255.255.0

######查看管理节点MHA切换日志
[[email protected] ~]# tail /var/log/masterha/app1/manager.log
Invalidated master IP address on vdbsrv1(192.168.1.6:3306)
The latest slave vdbsrv2(192.168.1.7:3306) has all relay logs for recovery.
Selected vdbsrv2(192.168.1.7:3306) as a new master.
vdbsrv2(192.168.1.7:3306): OK: Applying all logs succeeded.
vdbsrv2(192.168.1.7:3306): OK: Activated master IP address.
vdbsrv3(192.168.1.8:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
vdbsrv3(192.168.1.8:3306): OK: Applying all logs succeeded. Slave started, replicating from vdbsrv2(192.168.1.7:3306)
vdbsrv2(192.168.1.7:3306): Resetting slave info succeeded.
Master failover to vdbsrv2(192.168.1.7:3306) completed successfully.

3、VIP切换perl脚本

????[[email protected] app1]# more /tmp/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.1.13/24‘;
my $key = ‘0‘;
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$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" ) {

        my $exit_code = 1;
        eval {
            print "Disabling the VIP on old master: $orig_master_host \n";
            &stop_vip();
            $exit_code = 0;
        };
        if ([email protected]) {
            warn "Got Error: [email protected]\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 ([email protected]) {
            warn [email protected];
            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() {
     return 0  unless  ($ssh_user);
    `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";
}
时间: 2024-10-07 13:08:52

MHA VIP切换脚本的相关文章

MHA在线切换脚本master_ip_online_change结合VIP

结合vip的主库在线切换脚本master_ip_online_change: #!/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 Fr

MHA 自动切换步骤及过程剖析

MHA是众多使用MySQL数据库企业高可用的不二选择,它简单易用,功能强大,实现了基于MySQL replication架构的自动主从故障转移,本文主要描述了MHA自动切换的步骤,对切换过程做了演示以及进行了适当的分析,供大家参考和理解MHA以及MySQL的原理. 1.MHA自动切换的步骤a.MHA manager启动时的校验阶段   根据配置文件校验复制配置以及识别当前的master   导致监控终止情形:复制配置异常,存在的异常slave,一些需要的脚本脚本异常   MHA manager启

mysql+keepalived主从切换脚本 转

Keepalived MySQL故障自动切换脚本 MySQL架构为master-slave(主从),master故障自动切换到slave上.当然也可以设置为双master,但这里有个弊端:就是当主的压力很大时,从上延时很大,比如落后2000秒,此时主挂了,从接管(VIP漂移到从),用户刚才发表的文章,此时因为同步延时大,还没复制过来,于是用户又发表了一篇文章,当原来的master修好后,因从的IO和SQL线程还在开启状态,还会继续同步刚才没有同步复制完的数据,这时有可能把用户新发表的文章更改掉,

MHA在线切换过程

MySQL MHA 在线切换是MHA除了自动监控切换换提供的另外一种方式,多用于诸如硬件升级,MySQL数据库迁移等等.该方式提供快速切换和优雅的阻塞写入,无关关闭原有服务器,整个切换过程在0.5-2s 的时间左右,大大减少了停机时间.本文演示了MHA 在线切换并给出了在线切换的基本步骤. 1.MHA在线切换方式及要求    $ masterha_master_switch --master_state=alive --conf=/etc/app1.cnf --new_master_host=h

254个VIP时脚本生成keepalived.conf配置文件

部署LVS+Keepalived高可用时,因业务需求一共要配置254个VIP,每个VIP有9台真实server,每台真实server还要配置些参数.如果手动写,要累死去了.所以编写了如下生成配置脚本.这个脚本生成的文件,在另一台高可用LVS上使用时,需要交换两个实例中的state与priority参数,成互为主从状态. 脚本一.自动生成keepalived.conf文件  #!/bin/sh #author:  by yangrong 2014-11-09 #function: auto bui

[MHA]master_ip_failover 测试可以使用的IP 地址切换脚本

#!/usr/bin/env perluse 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

mha切换脚本可用的

#!/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.237.120/24'; my $

MHA 主从切换过程及日志分析

本文主要在MHA 切换日志的角度分析MHA切换的过.MHA故障切换过程如下图所示 第一部分:开启MHA 监控 通过分析日志,得到以下步骤: 1.读取MHA manager 节点的配置文件,并检查配置文件中参数设置的正确性. Sat Jun 22 20:16:29 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Sat Jun 22 20:16:29 2019 - [

【MySQL】【高可用】从masterha_master_switch工具简单分析MHA的切换逻辑

简介:masterha_master_switch作为一个切换工具被集成在MHA程序包中, 安装:编译安装MHA manager后会在/usr/local/bin/中生成二进制可执行程序masterha_master_switch. 使用: $masterha_master_switch --help Usage: # For master failover masterha_master_switch --master_state=dead --global_conf=/etc/masterh