master_ip_failover.sh script is written in Perl, can be found in the Mha-manager source package, the following gives the combination of keepalived for automatic switching script:
[email protected] scripts]# cat master_ip_failover.sh
#!/usr/bin/env Perl
# Copyright (C) DeNA Co.,ltd.
#
# is the 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.
#
# distributed in the hope that it'll 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 has received a copy of the GNU general public License
# along with the program; If not, write to the free software
# Foundation, Inc.,
# Wuyi Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
# # Note:this is a sample script and are not complete. Modify the script based on your environment.
#!/usr/bin/env Perl
Use strict;
Use warnings FATAL = ' all ';
Use Getopt::long;
Use MHA::D bhelper;
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.56.188 ';
My $ssh _start_vip = "/etc/init.d/keepalived start";
My $ssh _STOP_VIP = "/etc/init.d/keepalived stop";
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 ";
# ' ssh $ssh _user\ @cluster1 \ "$ssh _start_vip \";
Exit 0;
}
else {
&usage ();
Exit 1;
}
}
# A Simple system call to enable the VIP on the new master
Sub Start_vip () {
' SSH $ssh _user\@ $new _master_host \ "$ssh _start_vip \";
}
# A Simple system Call this disable the VIP on the Old_master
Sub Stop_vip () {
My $ssh _user = "root";
' 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 ";
}
MySQL MHA master_ip_failover.sh Script