1.当主挂了,然后手动通过mha进行主从切换
masterha_stop --conf=/etc/mha/mysql_mha.cnf (在mha离线的情况下执行的)
masterha_master_switch --master_state=dead --conf=/etc/mha/mysql_mha.cnf --dead_master_host=192.168.209.131 --dead_master_port=33061 --new_master_host=192.168.209.132 --new_master_port=33061 --ignore_last_failover
(主挂了,主的vip不要动,直接在mha端执行上面语句,就会切换了)
(切换过程需要连续输入两次yes)
2.主没有挂,但是想主从切换
主没有挂,但是想对主进行版本升级之类的,需要把主切换到从
masterha_master_switch --conf=/etc/mha/mysql_mha.cnf --master_state=alive --new_master_host=192.168.209.132 --new_master_port=33061 --orig_master_is_new_slave --running_updates_limit=100
这里需要加上一个在线切换脚本,这个脚本用于在线切换的时候,把master上的vip,切换到slave上。这个脚本需要在配置文件上添加。
vim /etc/mha/mysql_mha.cnf
[server default]
manager_log=/home/mysql_mha/manager.log
manager_workdir=/home/mysql_mha
master_binlog_dir=/data/mysql/datanode1
remote_workdir=/home/mysql_mha
user=mha
password=123456
ping_interval=1
repl_user=rep
repl_password=123456
ssh_user=root
master_ip_failover_script=/etc/mha/master_ip_failover
#在线切换的脚本
master_ip_online_change_script=/etc/mha/master_ip_online_change
secondary_check_script=/usr/bin/masterha_secondary_check -s 192.168.209.128 -s 192.168.209.131 -s 192.168.209.132
[server1]
hostname=192.168.209.132
port=33061
candidate_master=1
[server2]
hostname=192.168.209.131
port=33061
candidate_master=1
[server3]
hostname=192.168.209.128
port=33061
no_master=1
脚本内容如下:
#!/bin/bash
source /root/.bash_profile
vip=`echo '192.168.209.199/24'` #设置VIP
key=`echo '0'`
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}'`
#要求服务的网卡识别名一样,都为ens192(这里是)
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 "Disabled thi 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"
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"
参考文章:
https://qiniu.wsfnk.com/%E7%AC%AC12%E8%AF%BE-4%E4%BC%81%E4%B8%9A%E5%B8%B8%E8%A7%81MySQL%E6%9E%B6%E6%9E%84%E5%BA%94%E7%94%A8%E5%AE%9E%E6%88%98%E4%B9%8BMHA%E6%9E%B6%E6%9E%84.pdf
https://boke.wsfnk.com/archives/537.html