MYSQL读写分离集群搭建(三)
mha初步搭建完成后,还有一个工作需要完成
就是当MyCat访问写master库时,应该是一个固定的地址
因此要配置mha,能生成一个master库所使用的ip地址
在MHA Manager配置中,如果实现MHA的故障切换需要在配置文件/etc/masterha_default.cnf 中启用下面参数:
master_ip_failover_script= #master failover时执行
shutdown_script= #master shutdown时执行
report_script= #生成报告时执行
master_ip_online_change_script= #master switchover时执行
下面脚本参考了网上的脚本,但是进行了修改,增加了mha启动时,在缺省的master库上开启vip的命令
vi /etc/masterha_default.cnf
master_ip_failover_script=/mha/failover.sh
master_ip_online_change_script=/mha/online.sh
当mha启动时,将执行master_ip_failover_script=/mha/failover.sh这行脚本
当mydb01宕机,向mydb02切换时,将执行master_ip_failover_script=/mha/failover.sh这个脚本
当手工从mydb02向mydb01切换时,将执行master_ip_online_change_script=/mha/online.sh这个脚本
vi /mha/failover.sh
#/bin/bash
source /root/.bash_profile
vip=`echo '8.1.3.188/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 ens33:$key down"`
start_vip=`echo "ssh root@$new_master_host /sbin/ifconfig ens33:$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\n\n"
fi
if [ $command = 'start' -o $command = 'status' ]
then
echo -e "\n\n\n***************************************************************\n"
if [[ $new_master_host == "" ]]
then
new_master_host=`echo "$3" | awk -F = '{print $2}'`
echo -e "Enabling the VIP - $vip on new master: $new_master_host \n"
new_command=`echo "ssh root@$new_master_host /sbin/ifconfig ens33:$key $vip"`
$new_command
else
echo -e "Enabling the VIP - $vip on new master: $new_master_host \n"
$start_vip
fi
if [ $? -eq 0 ]
then
echo "Enabled the VIP successfully"
else
echo "Enabled the VIP failed"
fi
echo -e "***************************************************************\n\n\n\n"
fi
chmod +x failover.sh
#vi online.sh
#/bin/bash
source /root/.bash_profile
vip=`echo '8.1.3.188/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 "$6" | 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 ens33:$key down"`
start_vip=`echo "ssh root@$new_master_host /sbin/ifconfig ens33:$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\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\n\n"
fi
chmod +x dbvip.sh
停止
masterha_stop --conf=/etc/masterha_default.cnf
再次运行
nohup masterha_manager --conf=/etc/masterha_default.cnf --ignore_last_failover < /dev/null > /root/nohupmanager.log 2>&1 &
进行测试:
场景一:启动mha manager,vip在mydb01上生成,正确
场景二:mydb01宕机,mha指导切换,vip在mydb02上生成,此时mydb02为master,mydb03、mydb04为slave,正确
场景三:mydb01开始,此时有两个master
在mydb01上,执行
mysql> change master to master_host='8.1.3.161',master_port=3306,master_user='root',master_password='zijiwu2020T';
mysql> start slave;
减少一个master,然后在mha manager上执行mha手工切换命令
#masterha_master_switch --conf=/etc/masterha_default.cnf --master_state=alive --new_master_host=mydb01 --orig_master_is_new_slave --running_updates_limit=10000 --interactive=0
vip将切换回mydb01上,此时mydb01是master,其余三台是slave
正确