How to use script for VIP switching in MHA
Editor to share with you how MHA to achieve VIP switching to use the script, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
In the MHA Manager-side configuration, the following three parameters need to be enabled in the configuration file / etc/masterha/app1/app1.cnf if vip failover for MHA is achieved:
Execute when master_ip_failover_script= / etc/masterha/app1/master_ip_failover # master failover
# shutdown_script= / etc/masterha/power_manager
Execute when report_script= / etc/masterha/app1/send_report # master failover
For more information on MHA configuration during master_ip_online_change_script=/etc/masterha/app1/master_ip_online_change # master switchover, please see: http://blog.csdn.net/lichangzai/article/details/50470771
The details of the script are as follows:
Master_ip_failover (perl) script
[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.21Universe; # 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\ nFlowers *\ n"
Print "Disabling the VIP-$vip on old master: $orig_master_host\ n"
Print "* *\ 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\ nFlowers *\ n"
Print "Enabling the VIP-$vip on new master: $new_master_host\ n"
Print "* *\ 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 {
"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) script
[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.21Universe; # 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\ nFlowers *\ n"
Print "Disabling the VIP-$vip on old master: $orig_master_host\ n"
Print "* *\ 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\ nFlowers *\ n"
Print "Enabling the VIP-$vip on new master: $new_master_host\ n"
Print "* *\ 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 {
"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) script
# the following is the master_ip_online_change (shell) script written by reuse
[root@host8 app1] # cat master_ip_online_change.sh
# / bin/bash
Source / root/.bash_profile
Vip= `echo '10.1.5.21Universe 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"
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"
Fi
Send_report (shell) script
[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}'`
# check whether there is a successfully at the end of the log. If so, the switch is successful and email is sent whether it is successful or not.
Tac / etc/masterha/app1/manager.log | sed-n 2p | grep 'successfully' > / dev/null
If [$?-eq 0]
Then
Echo-e "MHA $subject master-slave switch succeeded\ nmaster:$orig_master_host-- > $new_master_host\ n $body\ ncurrent slave library: $new_slave_hosts" | mutt
-s "MySQL instance down, MHA $subject switched successfully"-- 94097532@qq.com
Else
Echo-e "MHA $subject master-slave switch failed\ nmaster:$orig_master_host-- > $new_master_host\ n $body" | mutt-s "MySQL instance is down, MHA $subje
Ct handover failed "--94097532@qq.com
Fi
The above is all the content of the article "how to switch VIP to script in MHA". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!