MHA故障恢复

[root@db04 ~]# cat recovery_mha.sh
#!/bin/bash
. /etc/init.d/functions

slave_ip=("172.16.1.51" "172.16.1.52" "172.16.1.53" "172.16.1.54")
for runt in "${slave_ip[@]}";do
	ssh "$runt" "mysql -uroot -p123 -e \"grant all on *.* to root@'%' identified by '123';\""
done

mha_log="/etc/mha/logs/manager.log"
pass="123"
down_master=$(sed -nr 's#^M.* (.*)\(.*\).*!$#\1#gp' ${mha_log})
# 2.找到change master语句
change=$(grep -i 'change master to' ${mha_log} |awk -F: '{print $4}'|sed "s#xxx#$pass#g")
mha_user=mha
mha_pass=mha

# 1.修复宕机主库
ssh $down_master "systemctl start mysqld"
while true;do
	mysql_count=`ps -ef|grep -c [m]ysqld`
	if [ $mysql_count -gt 0 ];then
		action "$down_master MySQL Server" /bin/true
		# 3.在宕机主库中执行,开启主从复制
mysql -u${mha_user} -p${mha_pass} -h$down_master -e "${change};start slave;" &>/dev/null
		break
	fi
done

# 4.修复 MHA 配置文件
cat > /etc/mha/app1.cnf <<EOF
[server default]
manager_log=${mha_log}
manager_workdir=/etc/mha/app1
master_binlog_dir=/app/mysql/data
password=${mha_user}
ping_interval=2
repl_password=123
repl_user=rep
ssh_port=22
ssh_user=root
user=${mha_pass}

[server1]
hostname=172.16.1.51
port=3306
[server2]
hostname=172.16.1.52
port=3306
[server3]
hostname=172.16.1.53
port=3306
[server4]
hostname=172.16.1.54
port=3306
EOF
posted @ 2023-10-09 08:34  普里莫  阅读(25)  评论(0编辑  收藏  举报