Nginx+PHP+MySQL双机互备、全自动切换方案

2019-10-15 22:07:08王冬梅

then
#取得与内网VIP绑定的服务器内网IP
eth0_active_server=$(${mysql_bin} -u"${mysql_username}" -p"${mysql_password}" -P"${mysql_port}" -h"${vip_eth0_share}" -e"show slave statusG" | grep "Master_Host" | awk -F ': ' '{printf $2}')

#如果内网VIP=主机内网IP(主机MySQL中的Master_Host显示的是备机的域名或IP),且本机为主机
if [ "${eth0_active_server}" = "${rip_eth0_slave}" ] && [ "${type}" = "master" ]
then
function_rsync_master_to_slave
function_vip_arping
#如果内网VIP=备机内网IP(备机MySQL中的Master_Host显示的是主机的域名或IP)
elif [ "${eth0_active_server}" = "${rip_eth0_master}" ]
then
if (curl -m 30 -G http://${rip_eth1_master}/ > /dev/null 2>&1) && (${mysql_bin} -u"${mysql_username}" -p"${mysql_password}" -P"${mysql_port}" -h"${rip_eth0_master}" -e"show slave statusG" | grep "Seconds_Behind_Master: 0" > /dev/null 2>&1)
then
#如果主机能够访问,数据库同步无延迟,且本机就是主机,那么由本机绑定虚拟IP
if [ "${type}" = "master" ]
then
#如果本机为主机
function_bind_vip
function_vip_arping
echo "${date} 主机已绑定虚拟IP!(Type:1)" >> ${logfile}
else
#如果本机为备机
function_remove_vip
echo "${date} 备机已去除虚拟IP!(Type:2)" >> ${logfile}
fi
else
if [ "${type}" = "slave" ]
then
#如果本机为备机
function_rsync_slave_to_master
function_vip_arping
fi
fi
fi
else
#虚拟IP无法访问时,判断主机能否访问
if (curl -m 30 -G http://${rip_eth1_master}/ > /dev/null 2>&1) && (${mysql_bin} -u"${mysql_username}" -p"${mysql_password}" -P"${mysql_port}" -h"${rip_eth0_master}" -e"show slave statusG" > /dev/null 2>&1)
then
#如果主机能够访问,且本机就是主机,那么由本机绑定虚拟IP
if [ "${type}" = "master" ]
then
function_bind_vip
function_vip_arping
echo "${date} 主机已绑定虚拟IP!(Type:3)" >> ${logfile}
else
function_remove_vip
echo "${date} 备机已去除虚拟IP!(Type:4)" >> ${logfile}
fi
elif (curl -m 30 -G http://${rip_eth1_slave}/ > /dev/null 2>&1) && (${mysql_bin} -u"${mysql_username}" -p"${mysql_password}" -P"${mysql_port}" -h"${rip_eth0_slave}" -e"show slave statusG" > /dev/null 2>&1)
then
#如果主机不能访问而备机能够访问,且本机就是备机,那么由备机绑定虚拟IP
if [ "${type}" = "slave" ]
then
function_bind_vip
function_vip_arping
echo "${date} 备机已绑定虚拟IP!(Type:5)" >> ${logfile}
else
function_remove_vip
echo "${date} 主机已去除虚拟IP!(Type:6)" >> ${logfile}
fi
else
echo "${date} 主机、备机全部无法访问!(Type:7)" >> ${logfile}
fi
fi
#每次循环暂停20秒(即间隔20秒检测一次)
sleep 20
done