MYSQL之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


首先要在主机(主)上添加一个VIP,然后当主的服务出现异常的时候,自动利用脚本把有故障的主的VIPdown掉,然后把VIP的权限给另外一个

新的主服务。

假设VIP为10.1.5.21

/sbin/ifconfig eth0:1  10.1.5.21/24

脚本具体内容如下:

master_ip_failover(perl)脚本


[root@host8 app1]# cat master_ip_failover

[plain]  view plain  copy
  1. #!/usr/bin/env perl  
  2. use strict;  
  3. use warnings FATAL =>'all';  
  4.   
  5. use Getopt::Long;  
  6.   
  7. my (  
  8. $command,          $ssh_user,        $orig_master_host, $orig_master_ip,  
  9. $orig_master_port, $new_master_host, $new_master_ip,    $new_master_port  
  10. );  
  11.   
  12. my $vip = '10.1.5.21/24';  # Virtual IP  
  13. my $key = "1";  
  14. my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";  
  15. my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";  
  16. my $exit_code = 0;  
  17.   
  18. GetOptions(  
  19. 'command=s'          => \$command,  
  20. 'ssh_user=s'         => \$ssh_user,  
  21. 'orig_master_host=s' => \$orig_master_host,  
  22. 'orig_master_ip=s'   => \$orig_master_ip,  
  23. 'orig_master_port=i' => \$orig_master_port,  
  24. 'new_master_host=s'  => \$new_master_host,  
  25. 'new_master_ip=s'    => \$new_master_ip,  
  26. 'new_master_port=i'  => \$new_master_port,  
  27. );  
  28.   
  29. exit &main();  
  30.   
  31. sub main {  
  32.   
  33. #print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";  
  34.   
  35. if ( $command eq "stop" || $command eq "stopssh" ) {  
  36.   
  37.         # $orig_master_host, $orig_master_ip, $orig_master_port are passed.  
  38.         # If you manage master ip address at global catalog database,  
  39.         # invalidate orig_master_ip here.  
  40.         my $exit_code = 1;  
  41.         eval {  
  42.             print "\n\n\n***************************************************************\n";  
  43.             print "Disabling the VIP - $vip on old master: $orig_master_host\n";  
  44.             print "***************************************************************\n\n\n\n";  
  45. &stop_vip();  
  46.             $exit_code = 0;  
  47.         };  
  48.         if ($@) {  
  49.             warn "Got Error: $@\n";  
  50.             exit $exit_code;  
  51.         }  
  52.         exit $exit_code;  
  53. }  
  54. elsif ( $command eq "start" ) {  
  55.   
  56.         # all arguments are passed.  
  57.         # If you manage master ip address at global catalog database,  
  58.         # activate new_master_ip here.  
  59.         # You can also grant write access (create user, set read_only=0, etc) here.  
  60. my $exit_code = 10;  
  61.         eval {  
  62.             print "\n\n\n***************************************************************\n";  
  63.             print "Enabling the VIP - $vip on new master: $new_master_host \n";  
  64.             print "***************************************************************\n\n\n\n";  
  65. &start_vip();  
  66.             $exit_code = 0;  
  67.         };  
  68.         if ($@) {  
  69.             warn $@;  
  70.             exit $exit_code;  
  71.         }  
  72.         exit $exit_code;  
  73. }  
  74. elsif ( $command eq "status" ) {  
  75.         print "Checking the Status of the script.. OK \n";  
  76.         `ssh $ssh_user\@$orig_master_host \" $ssh_start_vip \"`;  
  77.         exit 0;  
  78. }  
  79. else {  
  80. &usage();  
  81.         exit 1;  
  82. }  
  83. }  
  84.   
  85. # A simple system call that enable the VIP on the new master  
  86. sub start_vip() {  
  87. `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;  
  88. }  
  89. # A simple system call that disable the VIP on the old_master  
  90. sub stop_vip() {  
  91. `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;  
  92. }  
  93.   
  94. sub usage {  
  95. print  
  96. "Usage: master_ip_failover –command=start|stop|stopssh|status –orig_master_host=host –orig_master_ip=ip –orig_master_port=po  
  97. rt –new_master_host=host –new_master_ip=ip –new_master_port=port\n";  
  98. }  


master_ip_online_change(perl)脚本


[root@host8 app1]# cat master_ip_online_change

[plain]  view plain  copy
  1. #!/usr/bin/env perl  
  2. use strict;  
  3. use warnings FATAL =>'all';  
  4.   
  5. use Getopt::Long;  
  6.   
  7. my $vip = '10.1.5.21/24';  # Virtual IP  
  8. my $key = "1";  
  9. my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";  
  10. my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";  
  11. my $exit_code = 0;  
  12.   
  13. my (  
  14.   $command,              $orig_master_is_new_slave, $orig_master_host,  
  15.   $orig_master_ip,       $orig_master_port,         $orig_master_user,  
  16.   $orig_master_password, $orig_master_ssh_user,     $new_master_host,  
  17.   $new_master_ip,        $new_master_port,          $new_master_user,  
  18.   $new_master_password,  $new_master_ssh_user,  
  19. );  
  20. GetOptions(  
  21.   'command=s'                => \$command,  
  22.   'orig_master_is_new_slave' => \$orig_master_is_new_slave,  
  23.   'orig_master_host=s'       => \$orig_master_host,  
  24.   'orig_master_ip=s'         => \$orig_master_ip,  
  25.   'orig_master_port=i'       => \$orig_master_port,  
  26.   'orig_master_user=s'       => \$orig_master_user,  
  27.   'orig_master_password=s'   => \$orig_master_password,  
  28.   'orig_master_ssh_user=s'   => \$orig_master_ssh_user,  
  29.   'new_master_host=s'        => \$new_master_host,  
  30.   'new_master_ip=s'          => \$new_master_ip,  
  31.   'new_master_port=i'        => \$new_master_port,  
  32.   'new_master_user=s'        => \$new_master_user,  
  33.   'new_master_password=s'    => \$new_master_password,  
  34.   'new_master_ssh_user=s'    => \$new_master_ssh_user,  
  35. );  
  36.   
  37.   
  38. exit &main();  
  39.   
  40. sub main {  
  41.   
  42. #print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";  
  43.   
  44. if ( $command eq "stop" || $command eq "stopssh" ) {  
  45.   
  46.         # $orig_master_host, $orig_master_ip, $orig_master_port are passed.  
  47.         # If you manage master ip address at global catalog database,  
  48.         # invalidate orig_master_ip here.  
  49.         my $exit_code = 1;  
  50.         eval {  
  51.             print "\n\n\n***************************************************************\n";  
  52.             print "Disabling the VIP - $vip on old master: $orig_master_host\n";  
  53.             print "***************************************************************\n\n\n\n";  
  54. &stop_vip();  
  55.             $exit_code = 0;  
  56.         };  
  57.         if ($@) {  
  58.             warn "Got Error: $@\n";  
  59.             exit $exit_code;  
  60.         }  
  61.         exit $exit_code;  
  62. }  
  63. elsif ( $command eq "start" ) {  
  64.   
  65.         # all arguments are passed.  
  66.         # If you manage master ip address at global catalog database,  
  67.         # activate new_master_ip here.  
  68.         # You can also grant write access (create user, set read_only=0, etc) here.  
  69. my $exit_code = 10;  
  70.         eval {  
  71.             print "\n\n\n***************************************************************\n";  
  72.             print "Enabling the VIP - $vip on new master: $new_master_host \n";  
  73.             print "***************************************************************\n\n\n\n";  
  74. &start_vip();  
  75.             $exit_code = 0;  
  76.         };  
  77.         if ($@) {  
  78.             warn $@;  
  79.             exit $exit_code;  
  80.         }  
  81.         exit $exit_code;  
  82. }  
  83. elsif ( $command eq "status" ) {  
  84.         print "Checking the Status of the script.. OK \n";  
  85.         `ssh $orig_master_ssh_user\@$orig_master_host \" $ssh_start_vip \"`;  
  86.         exit 0;  
  87. }  
  88. else {  
  89. &usage();  
  90.         exit 1;  
  91. }  
  92. }  
  93.   
  94. # A simple system call that enable the VIP on the new master  
  95. sub start_vip() {  
  96. `ssh $new_master_ssh_user\@$new_master_host \" $ssh_start_vip \"`;  
  97. }  
  98. # A simple system call that disable the VIP on the old_master  
  99. sub stop_vip() {  
  100. `ssh $orig_master_ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;  
  101. }  
  102.   
  103. sub usage {  
  104. print  
  105. "Usage: master_ip_failover –command=start|stop|stopssh|status –orig_master_host=host –orig_master_ip=ip –orig_master_port=po  
  106. rt –new_master_host=host –new_master_ip=ip –new_master_port=port\n";  
  107. }  


master_ip_online_change(shell)脚本


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

[root@host8 app1]# cat master_ip_online_change.sh

[plain]  view plain  copy
  1. #/bin/bash  
  2. source /root/.bash_profile  
  3.   
  4. vip=`echo '10.1.5.21/24'`  # Virtual IP  
  5. key=`echo '1'`  
  6.   
  7. command=`echo "$1" | awk -F = '{print $2}'`  
  8. orig_master_host=`echo "$2" | awk -F = '{print $2}'`  
  9. new_master_host=`echo "$7" | awk -F = '{print $2}'`  
  10. orig_master_ssh_user=`echo "${12}" | awk -F = '{print $2}'`  
  11. new_master_ssh_user=`echo "${13}" | awk -F = '{print $2}'`  
  12.   
  13. stop_vip=`echo "ssh root@$orig_master_host /sbin/ifconfig  eth0:$key  down"`  
  14. start_vip=`echo "ssh root@$new_master_host /sbin/ifconfig  eth0:$key  $vip"`  
  15.   
  16. if [ $command = 'stop' ]  
  17.    then  
  18.    echo -e "\n\n\n***************************************************************\n"  
  19.    echo -e "Disabling the VIP - $vip on old master: $orig_master_host\n"  
  20.    $stop_vip  
  21.    if [ $? -eq 0 ]  
  22.       then  
  23.       echo "Disabled the VIP successfully"  
  24.    else  
  25.       echo "Disabled the VIP failed"  
  26.    fi  
  27.    echo -e "***************************************************************\n\n\n\n"  
  28. fi  
  29.   
  30. if [ $command = 'start' -o $command = 'status' ]  
  31.    then  
  32.    echo -e "\n\n\n***************************************************************\n"  
  33.    echo -e "Enabling the VIP - $vip on new master: $new_master_host \n"  
  34.    $start_vip  
  35.    if [ $? -eq 0 ]  
  36.       then  
  37.       echo "Enabled the VIP successfully"  
  38.    else  
  39.       echo "Enabled the VIP failed"  
  40.    fi  
  41.    echo -e "***************************************************************\n\n\n\n"  
  42. fi  


send_report(shell)脚本


[root@host8 app1]# cat send_report

[plain]  view plain  copy
  1. #/bin/bash  
  2. source /root/.bash_profile  
  3.   
  4. orig_master_host=`echo "$1" | awk -F = '{print $2}'`  
  5. new_master_host=`echo "$2" | awk -F = '{print $2}'`  
  6. new_slave_hosts=`echo "$3" | awk -F = '{print $2}'`  
  7. subject=`echo "$4" | awk -F = '{print $2}'`  
  8. body=`echo "$5" | awk -F = '{print $2}'`  
  9.   
  10. #判断日志结尾是否有successfully,有则表示切换成功,成功与否都发邮件。  
  11. tac /etc/masterha/app1/manager.log | sed -n 2p | grep 'successfully' > /dev/null  
  12. if [ $? -eq 0 ]  
  13.     then  
  14.     echo -e "MHA $subject 主从切换成功\n master:$orig_master_host --> $new_master_host \n $body \n 当前从库:$new_slave_hosts" | mutt  
  15.  -s "MySQL实例宕掉,MHA $subject 切换成功" -- 94097532@qq.com  
  16. else  
  17.     echo -e "MHA $subject 主从切换失败\n master:$orig_master_host --> $new_master_host \n $body" | mutt -s "MySQL实例宕掉,MHA $subje  
  18. ct 切换失败" -- 94097532@qq.com  
  19. fi  






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值