From f2017b27870e9e336a23c92895f640f9d7ade01a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BB=BA=E6=98=8C?= Date: Tue, 17 Mar 2020 11:44:47 +0800 Subject: [PATCH 1/2] add ssh_port parameter in master_ip_failover. --- samples/scripts/master_ip_failover | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/samples/scripts/master_ip_failover b/samples/scripts/master_ip_failover index 491902e..33c7a35 100755 --- a/samples/scripts/master_ip_failover +++ b/samples/scripts/master_ip_failover @@ -26,7 +26,7 @@ use Getopt::Long; use MHA::DBHelper; my ( - $command, $ssh_user, $orig_master_host, + $command, $ssh_user, $ssh_port, $orig_master_host, $orig_master_ip, $orig_master_port, $new_master_host, $new_master_ip, $new_master_port, $new_master_user, $new_master_password @@ -34,6 +34,7 @@ my ( GetOptions( 'command=s' => \$command, 'ssh_user=s' => \$ssh_user, + 'ssh_port=s' => \$ssh_port, 'orig_master_host=s' => \$orig_master_host, 'orig_master_ip=s' => \$orig_master_ip, 'orig_master_port=i' => \$orig_master_port, @@ -115,6 +116,6 @@ sub main { 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"; +"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 --ssh_user=user --ssh_port=ssh_port\n"; } From 81d70af6ebf6b93694e0ea791969d999ad2ad2b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BB=BA=E6=98=8C?= Date: Wed, 24 Jun 2020 11:20:02 +0800 Subject: [PATCH 2/2] an example for master_ip_failover. --- samples/scripts/master_ip_failover | 166 ++++++++++++----------------- 1 file changed, 68 insertions(+), 98 deletions(-) diff --git a/samples/scripts/master_ip_failover b/samples/scripts/master_ip_failover index 491902e..c8baf03 100755 --- a/samples/scripts/master_ip_failover +++ b/samples/scripts/master_ip_failover @@ -1,120 +1,90 @@ #!/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 + $command, $ssh_user, $orig_master_host, $orig_master_ip, + $orig_master_port, $new_master_host, $new_master_ip, $new_master_port ); + +my $vip = '172.31.0.22'; # Virtual IP +my $key = "1"; +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, - 'new_master_user=s' => \$new_master_user, - 'new_master_password=s' => \$new_master_password, + '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 { - 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; + 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 "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; } - 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 { - my $new_master_handler = new MHA::DBHelper(); - - # args: hostname, port, user, password, raise_error_or_not - $new_master_handler->connect( $new_master_ip, $new_master_port, - $new_master_user, $new_master_password, 1 ); - - ## Set read_only=0 on the new master - $new_master_handler->disable_log_bin_local(); - print "Set read_only=0 on the new master.\n"; - $new_master_handler->disable_read_only(); - - ## Creating an app user on the new master - print "Creating app user on the new master..\n"; - FIXME_xxx_create_user( $new_master_handler->{dbh} ); - $new_master_handler->enable_log_bin_local(); - $new_master_handler->disconnect(); - - ## Update master ip on the catalog database, etc - FIXME_xxx; - - $exit_code = 0; - }; - if ($@) { - warn $@; - - # If you want to continue failover, exit 10. - 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(); + $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 -p 10220 \" $ssh_start_vip \"`; + exit 0; } - exit $exit_code; - } - elsif ( $command eq "status" ) { + else { + &usage(); + exit 1; + } +} - # do nothing - 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 -p 11221 \" $ssh_start_vip \"`; +} +# A simple system call that disable the VIP on the old_master +sub stop_vip() { + `ssh $ssh_user\@$orig_master_host -p 11221 \" $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"; + 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"; } -