您好,欢迎访问一九零五行业门户网

批量查看mysql多从状态和修改多从主库指向

本脚本主要解决批量查看mysql多从状态和修改多从主库指向,并打印出执行结果。适用于主库没有做高可用或是做高可用但是v-ip没有漂移到新的主库上的问题。代码如
本脚本主要解决批量查看mysql多从状态和修改多从主库指向,,并打印出执行结果。适用于主库没有做高可用或是做高可用但是v-ip没有漂移到新的主库上的问题。代码如下:
#!/usr/bin/env python# -*- coding: utf-8 -*-import mysqldb,sys,os,threading,timeuser = 'root'passwd = '1q2w3e4r'#mysql执行change master命令的用户名和密码def log_w(text):#写日志logfile = slave_res.txtf = open(logfile,'a+')f.write(text)f.close()def db_conn(host,res,flag):text = ###################_____%s_____###################\n\n % hosttry:conn = mysqldb.connect(host = host,port = 6006,user = user,passwd = passwd,charset=utf8,connect_timeout = 5)cursor = conn.cursor(cursorclass = mysqldb.cursors.dictcursor)sql = '''show slave status'''cursor.execute(sql)#查看当前同步信息alldata = cursor.fetchall()if len(alldata) != 0:#如果没有同步信息则抛错,退出if alldata[0]['master_log_file']==alldata[0]['relay_master_log_file'] and alldata[0]['read_master_log_pos']==alldata[0]['exec_master_log_pos']:text = text + ok + '\t' + 'master_host:' + str(alldata[0]['master_host']) + ' ' + str(alldata[0]['master_log_file']) + ' ' + str(alldata[0]['relay_master_log_file']) + ' ' + str(alldata[0]['read_master_log_pos']) + ' ' + str(alldata[0]['exec_master_log_pos']) + ' ' + str(alldata[0]['seconds_behind_master'])+'\n'if flag == '1':try:sql = stop slave;cursor.execute(sql)#停止从库同步except exception, e:passsql = '''change master to master_host='192.10.100.100',master_user='rep_slave',master_password='reemakereplication6210',master_port=6006,master_log_file='mysql-bin.000100',master_log_pos=300;'''cursor.execute(sql)#执行change master语句sql = start slave;cursor.execute(sql)#开启同步sql = 'show slave status'cursor.execute(sql)#查看最新的同步信息alldata = cursor.fetchall()if (alldata[0]['slave_io_running'] == 'yes') and (alldata[0]['slave_sql_running'] == 'yes'):text = text + ok + '\t' + 'master_host:' + str(alldata[0]['master_host']) + ' ' + str(alldata[0]['master_log_file']) + ' ' + str(alldata[0]['relay_master_log_file']) + ' ' + str(alldata[0]['read_master_log_pos']) + ' ' + str(alldata[0]['exec_master_log_pos']) + ' ' + str(alldata[0]['seconds_behind_master'])+'\n'else:text = text + start slave error + '\t' + 'master_host:' + str(alldata[0]['master_host']) + '\t' + 'slave_io_running: '+str(alldata[0]['slave_io_running']) + '\t' + 'slave_sql_running:' + str(alldata[0]['slave_sql_running']) + '\n'else:text = text + slave error + ' ' + 'master_host:' + str(alldata[0]['master_host']) + ' ' + str(alldata[0]['master_log_file']) + ' ' + str(alldata[0]['relay_master_log_file']) + ' ' + str(alldata[0]['read_master_log_pos']) + ' ' + str(alldata[0]['exec_master_log_pos']) + str(alldata[0]['seconds_behind_master'])+'\n'else:text = text + error,this host not set slave informationcursor.close()conn.close()except exception, e:text = text + error + '\t' + str(e)res.append(text)def start(flag):threads = []res = []host_list = ['192.168.1.114','192.168.1.120']for host in host_list:t = threading.thread(target=db_conn,args=(host,res,flag))t.setdaemon(true)threads.append(t)for i in range(len(threads)):threads[i].start()time.sleep(0.1)for i in range(len(threads)):threads[i].join()for i in res:if error in i:print \033[1;31;40m%s\033[0m % ielse:print ilog_w(i)if flag == '1':text = \nchange master finishedprint textlog_w(text)else:text = \nshow slave status completeprint textlog_w(text)text = \n\n################### %s ###################\n\n % time.strftime(%y-%m-%d %h:%m:%s)print textlog_w(text)def main():printprint 请选择操作类型:\n\n0:查看所有从库的同步状态\n1:改变所有从库的主库指向\n#.decode(utf-8).encode(gbk)for i in range(3):choose = raw_input('your choose : ')if choose == '0' or choose == '1':start(choose)breakelse:print error,please enter right noumber again .printif __name__=='__main__':main()
其它类似信息

推荐信息