1. 程式人生 > >MHA 故障庫恢復到集群 python腳本

MHA 故障庫恢復到集群 python腳本

進行 gre 節點 logfile art health base ddd nbsp

#!/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

MHA 故障庫恢復到集群 python腳本