iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >MHA如何实现VIP切换用到脚本
  • 875
分享到

MHA如何实现VIP切换用到脚本

2024-04-02 19:04:59 875人浏览 八月长安
摘要

小编给大家分享一下MHA如何实现VIP切换用到脚本,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!在MHA Manager端配置中

小编给大家分享一下MHA如何实现VIP切换用到脚本,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

在MHA Manager端配置中,如果实现MHA的vip故障切换需要在配置文件/etc/masterha/app1/app1.cnf 中启用下面三个参数:


master_ip_failover_script= /etc/masterha/app1/master_ip_failover   #master failover时执行
#shutdown_script= /etc/masterha/power_manager
report_script= /etc/masterha/app1/send_report    #master failover时执行
master_ip_online_change_script=/etc/masterha/app1/master_ip_online_change   #master switchover时执行 MHA配置见: Http://blog.csdn.net/lichangzai/article/details/50470771

脚本具体内容如下:

master_ip_failover(perl)脚本


[root@host8 app1]# cat 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.1.5.21/24';  # Virtual IP

my $key = "1";

my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";

my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";

my $exit_code = 0;

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 "\n\n\n***************************************************************\n";

            print "Disabling the VIP - $vip on old master: $orig_master_host\n";

            print "***************************************************************\n\n\n\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 "\n\n\n***************************************************************\n";

            print "Enabling the VIP - $vip on new master: $new_master_host \n";

            print "***************************************************************\n\n\n\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=po

rt –new_master_host=host –new_master_ip=ip –new_master_port=port\n";

}

master_ip_online_change(perl)脚本

[root@host8 app1]# cat master_ip_online_change

#!/usr/bin/env perl

use strict;

use warnings FATAL =>'all';

use Getopt::Long;

my $vip = '10.1.5.21/24';  # Virtual IP

my $key = "1";

my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";

my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";

my $exit_code = 0;

my (

  $command,              $orig_master_is_new_slave, $orig_master_host,

  $orig_master_ip,       $orig_master_port,         $orig_master_user,

  $orig_master_passWord, $orig_master_ssh_user,     $new_master_host,

  $new_master_ip,        $new_master_port,          $new_master_user,

  $new_master_password,  $new_master_ssh_user,

);

GetOptions(

  'command=s'                => \$command,

  'orig_master_is_new_slave' => \$orig_master_is_new_slave,

  'orig_master_host=s'       => \$orig_master_host,

  'orig_master_ip=s'         => \$orig_master_ip,

  'orig_master_port=i'       => \$orig_master_port,

  'orig_master_user=s'       => \$orig_master_user,

  'orig_master_password=s'   => \$orig_master_password,

  'orig_master_ssh_user=s'   => \$orig_master_ssh_user,

  '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,

  'new_master_ssh_user=s'    => \$new_master_ssh_user,

);

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 "\n\n\n***************************************************************\n";

            print "Disabling the VIP - $vip on old master: $orig_master_host\n";

            print "***************************************************************\n\n\n\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 "\n\n\n***************************************************************\n";

            print "Enabling the VIP - $vip on new master: $new_master_host \n";

            print "***************************************************************\n\n\n\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 $orig_master_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 $new_master_ssh_user\@$new_master_host \" $ssh_start_vip \"`;

}

# A simple system call that disable the VIP on the old_master

sub stop_vip() {

`ssh $orig_master_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=po

rt –new_master_host=host –new_master_ip=ip –new_master_port=port\n";

}

master_ip_online_change(shell)脚本

#以下是重用写的master_ip_online_change(shell)脚本

[root@host8 app1]# cat master_ip_online_change.sh

#/bin/bash

source /root/.bash_profile

vip=`echo '10.1.5.21/24'`  # Virtual IP

key=`echo '1'`

command=`echo "$1" | awk -F = '{print $2}'`

orig_master_host=`echo "$2" | awk -F = '{print $2}'`

new_master_host=`echo "$7" | awk -F = '{print $2}'`

orig_master_ssh_user=`echo "${12}" | awk -F = '{print $2}'`

new_master_ssh_user=`echo "${13}" | awk -F = '{print $2}'`

stop_vip=`echo "ssh root@$orig_master_host /sbin/ifconfig  eth0:$key  down"`

start_vip=`echo "ssh root@$new_master_host /sbin/ifconfig  eth0:$key  $vip"`

if [ $command = 'stop' ]

   then

   echo -e "\n\n\n***************************************************************\n"

   echo -e "Disabling the VIP - $vip on old master: $orig_master_host\n"

   $stop_vip

   if [ $? -eq 0 ]

      then

      echo "Disabled the VIP successfully"

   else

      echo "Disabled the VIP failed"

   fi

   echo -e "***************************************************************\n\n\n\n"

fi

if [ $command = 'start' -o $command = 'status' ]

   then

   echo -e "\n\n\n***************************************************************\n"

   echo -e "Enabling the VIP - $vip on new master: $new_master_host \n"

   $start_vip

   if [ $? -eq 0 ]

      then

      echo "Enabled the VIP successfully"

   else

      echo "Enabled the VIP failed"

   fi

   echo -e "***************************************************************\n\n\n\n"

fi

send_report(shell)脚本

[root@host8 app1]# cat send_report

#/bin/bash

source /root/.bash_profile

orig_master_host=`echo "$1" | awk -F = '{print $2}'`

new_master_host=`echo "$2" | awk -F = '{print $2}'`

new_slave_hosts=`echo "$3" | awk -F = '{print $2}'`

subject=`echo "$4" | awk -F = '{print $2}'`

body=`echo "$5" | awk -F = '{print $2}'`

#判断日志结尾是否有successfully,有则表示切换成功,成功与否都发邮件。

tac /etc/masterha/app1/manager.log | sed -n 2p | grep 'successfully' > /dev/null

if [ $? -eq 0 ]

    then

    echo -e "MHA $subject 主从切换成功\n master:$orig_master_host --> $new_master_host \n $body \n 当前从库:$new_slave_hosts" | mutt

 -s "Mysql实例宕掉,MHA $subject 切换成功" -- 94097532@qq.com

else

    echo -e "MHA $subject 主从切换失败\n master:$orig_master_host --> $new_master_host \n $body" | mutt -s "mysql实例宕掉,MHA $subje

ct 切换失败" -- 94097532@qq.com

fi

以上是“MHA如何实现VIP切换用到脚本”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网数据库频道!

您可能感兴趣的文档:

--结束END--

本文标题: MHA如何实现VIP切换用到脚本

本文链接: https://www.lsjlt.com/news/62198.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • sql中外码怎么设置
    sql 中外码设置步骤:确定父表和子表。在子表中创建外码列,引用父表主键。使用 foreign key 约束将外码列链接到父表主键。指定引用动作,以处理父表数据更改时的子表数据操作。 ...
    99+
    2024-05-15
  • sql中having是什么
    having 子句用于过滤分组结果,应用于分组后的数据集。它与 where 子句类似,但基于分组结果而不是原始数据。用法:1. 过滤分组后的聚合值。2. 根据分组后的...
    99+
    2024-05-15
  • 在sql中空值用什么表示
    在 sql 中,空值表示未知或不存在的值,可使用 null、空字符串或特殊值表示。处理空值的方法包括使用操作符(is null/is not null)、coalesce 函数(返回第一...
    99+
    2024-05-15
    oracle
  • sql中number什么意思
    sql 中的 number 类型用于存储数值数据,包括小数和整数,特别适合货币、度量和科学数据。其精度由 scale(小数点位数)和 precision(整数字段和小数字段总位数)决定。...
    99+
    2024-05-15
  • sql中空值赋值为0怎么写
    可以通过使用 coalesce() 函数将 sql 中的空值替换为指定值(如 0)。coalesce() 的语法为 coalesce(expression, replacement),其...
    99+
    2024-05-15
  • sql中revoke语句的功能
    revoke 语句用于撤销指定用户或角色的权限或角色成员资格。可撤销的权限包括 select、insert、update、delete 等,撤销的对象类型包括表、视图、存储过程...
    99+
    2024-05-15
    敏感数据
  • sql中REVOKE是什么意思
    revoke 是 sql 中用于撤销用户或角色对数据库对象权限的命令。它通过撤销权限类型、对象级别和目标权限来实现:权限类型:撤销 select、insert、update、d...
    99+
    2024-05-15
  • sql中sp是什么意思
    sql中的sp是存储过程的缩写,它是一种预编译的、已命名的sql语句块,存储在数据库中,可以被用户通过简单命令调用。存储过程的特点有:可重用性、模块化、性能优化、安全性、事务支持。存储过...
    99+
    2024-05-15
    敏感数据
  • sql中references是什么意思
    sql 中的 references 关键字用于在外键约束中定义表之间的父-子关系。外键约束确保子表中的行都引用父表中存在的行,从而维护数据完整性。references 语法的格式为:fo...
    99+
    2024-05-15
  • sql中判断字段为空怎么写
    sql 中可通过 4 种方法判断字段是否为空:1)is null 运算符;2)is not null 运算符;3)coalesce() 函数;4)case 语句。例如,查询所有 colu...
    99+
    2024-05-15
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作