代码改变世界

MHA 故障库恢复到集群脚本

2017-08-04 15:12  ndzj  阅读(506)  评论(0编辑  收藏  举报

#!/bin/bash

#注 请把故障库启动后再执行此脚本

#1.获取mha日志中change语句
stmt=$(ssh exsddb3 'cat /var/log/masterha/app1/app1.log'|grep 'All other slaves'|grep "`date +%d|cut -d'0' -f2` "|cut -d':' -f4|awk '{sub(/^[ \t]+/, "")};1')

ip=`echo $stmt|cut -d' ' -f6|cut -d"'" -f1`
  pass=`echo $stmt|cut -d'=' -f6|cut -d"'" -f2` #复制用户
    dd='CHANGE MASTER TO MASTER_HOST'
      logfile=`echo $stmt|cut -d'=' -f4|cut -d"'" -f2`
    logpos=`echo $stmt|cut -d'=' -f5|cut -d',' -f1`
  user="MASTER_USER='replication'"
user='root'
passw='exsd2017727_SZ8*'

#2.转换成标准change语句
change=$(printf "$dd"'='"'""$ip""'","$user",'MASTER_PASSWORD='"'""$pass""'",'MASTER_LOG_FILE='"'""$logfile""'",'MASTER_LOG_POS='"$logpos")
  /usr/local/mysql/bin/mysql -u"$user" -p"$passw" -e 'show slave status\G'
sleep 1;printf ' '"%s %s\n"


#3.在故意库中change到新主库,并启动slave,查看salve状态
/usr/local/mysql/bin/mysql -u"$user" -p"$passw" -e "$change"
  /usr/local/mysql/bin/mysql -u"$user" -p"$passw" -e "start slave"
/usr/local/mysql/bin/mysql -u"$user" -p"$passw" -e 'show slave status\G'
sleep 2

#4.在manager节点上检查MHA复制状态
ssh exsddb3 '/usr/bin/masterha_check_repl --global_conf=/etc/masterha/masterha_default.conf --conf=/etc/masterha/app1.conf' >/tmp/check_repl

a=`cat /tmp/check_repl |grep 'MySQL Replication Health is OK.'`
#5.判断MHA集群是否OK,若OK就启动manager进程对MHA进行监控,然后查看MHA集群状态
if [[ "$a" = 'MySQL Replication Health is OK.' ]];then
  echo ' '
    echo ' '
      ssh exsddb3 'sh /sh/mha_start.sh'
   sleep 2
ssh exsddb3 '/usr/bin/masterha_check_status --global_conf=/etc/masterha/masterha_default.conf --conf=/etc/masterha/app1.conf'
else
  echo ' '
   echo ' '
echo 'MySQL Replication Health is not OK!'

#@_@ 欢迎交流!联系人 广西宾阳 阿桂 qq149951292