mysql mha恢复_MHA 故障库恢复到集群 python脚本

#!/usr/bin/env python

#! _*_ coding:utf-8 _*_

import os,re,time

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

#1.获取mha日志中change语句

stmt = os.popen("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'").read().strip('\n')

ddd = 'CHANGE MASTER TO MASTER_HOST='

ip = re.findall(r'\d+.\d+.\d+.\d+',stmt)[0]

userf = 'replication'

passwf = 'replication'

logfile = re.findall(r"MASTER_LOG_FILE='(.*)', MASTER_LOG",stmt)[0]

logpos = re.findall(r"MASTER_LOG_POS=(.*), MASTER_USER=",stmt)[0]

user = 'root'

passw = 'abc123'

mysqlbase = '/usr/local/mysql/bin/'

#2.转换成标准change语句

change = "%s'%s',MASTER_PASSWORD='%s',MASTER_PORT=%s,MASTER_LOG_FILE='%s',MASTER_LOG_POS=%s,MASTER_USER='%s'" %(ddd,ip,passwf,'3306',logfile,logpos,userf)

status = os.popen("%smysql -u'%s' -p'%s' -e 'show slave status\G'" %(mysqlbase,user,passw)).read()

print(' ')

#3.在故障库中change到新主库,并启动slave,查看salve状态

os.system("%smysql -u'%s' -p'%s' -e '%s'" %(mysqlbase,user,passw,change))

os.system("%smysql -u'%s' -p'%s' -e 'start slave'" %(mysqlbase,user,passw))

os.system("%smysql -u'%s' -p'%s' -e 'show slave status\G'" %(mysqlbase,user,passw))

#4.在manager节点上检查MHA复制状态

os.system("ssh exsddb3 '/usr/bin/masterha_check_repl --global_conf=/etc/masterha/masterha_default.conf --conf=/etc/masterha/app1.conf' >/tmp/check_repl")

print(' ')

print(' ')

ifok = os.popen("cat /tmp/check_repl |grep 'MySQL Replication Health is OK.'").read().strip('\n')

#5.判断MHA集群是否OK,若OK就启动manager进程对MHA进行监控,然后查看MHA集群状态

if ifok == 'MySQL Replication Health is OK.':

print(' ')

os.system("ssh exsddb3 'sh /sh/mha_start.sh'")

time.sleep(2)

os.system("ssh exsddb3 '/usr/bin/masterha_check_status --global_conf=/etc/masterha/masterha_default.conf --conf=/etc/masterha/app1.conf'")

else:

print(' ')

print('MySQL Replication Health is not OK!')

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值