MySQL database master switch from automated scripts

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import MySQLdb

import time

import sys

class m_s:

def __init__(self,host,user,password,port):

self.host=host

self.user=user

self.passowrd=password

self.port=port

def getConn(self,db="mysql"):

try:

conn=MySQLdb.connect(host=self.host, user=self.user, passwd=self.passowrd, db=db, port=self.port, charset="utf8")

cur = conn.cursor()

return cur

except Exception as e:

return e

def execSQLlock(self,*args):

flush_sql="FLUSH TABLES WITH READ LOCK"

cur.execute(flush_sql)

def execIo(self,cur,command):

cur.execute(command)

db_pos = cur.fetchall()

for value in db_pos:

value=value

return value

def exeStop(self,cur,command):

cur.execute(command)

db_pos = cur.fetchall()

return db_pos

def execSQLstatus(self,*args):

n=0

self.execSQLlock(cur)

flush_m ="flush logs"

cur.execute(flush_m)

while True:

data=[]

slave_pos=[]

n=n+1

exe_sql ="select Command,State,Info,Id from information_schema.processlist"

cur.execute(exe_sql)

plist = cur.fetchall()

for li in range(len(plist)):

if plist[li][0]=="Query" and plist[li][1]=="Waiting for global read lock":

lock_id ="kill " + str(plist[li][3])

print plist[li][2]

cur.execute(lock_id)

slave_pos.append(self.execIo(cur1, "show master status")[1])

data.append (self.execIo (Cur1, " Show Slave Status " ) [ . 6 ]) from the library cursor ##

time.sleep(1)

slave_pos.append(self.execIo(cur1, "show master status")[1])

data.append (self.execIo (CUR, " Show Master Status " ) [ . 1 ]) from the library cursor ##

print ".......",data,slave_pos

if data[0]==data[1] and slave_pos[0]==slave_pos[1]:

try:

Print " % s of times the data has been synchronized Analyzing ..... " % n-

if n==c_time:

Print " starts the switching operation from the master ........ "

self.exeStop (cur1, " STOP Slave " ) ## to stop the synchronization from the library

= self.exeStop The New_Pos (Cur1, " Show Master Status " ) and POS FILE # Get the value of the new primary database, the cursor is not switched from the library before

#Print " value acquiring new primary library FILE and POS, before the cursor is not switched from the library " , The New_Pos

self.exeStop (CUR, " the RESET Master; " ) ## is released from the main library information from the library main ......

## execution from the library new_change point to the new main library

self.exeStop(cur1,"reset slave all")

new_change="change master to master_host='"+str(args[1])+"'"+",master_user='"+args[2]+"'"+",master_password='"+args[3]+"',master_port="+str(args[4])+",MASTER_LOG_FILE='"+str(new_pos[0][0])+"'"+",MASTER_LOG_POS="+str(new_pos[0][1])

print new_change

self.exeStop (cur, new_change) ## change master to execute on the original main library .....

#Print " the implementation of change master to in the original main library .... "

self.exeStop (CUR, " Start Slave " ) ## change master to execute on the original main library .....

time.sleep(5)

s_pos=self.exeStop(cur,"show slave status;")

#print s_pos[0][10],s_pos[0][11]

if s_pos[0][10]=="Yes" and s_pos[0][11]=="Yes":

self.exeStop(cur1,"reset slave all")

Print " master-slave switching success! "

print "\n"

while True:

Print " waiting for the other operation is complete, the main library is about to unlock tables ...... "

try:

= the raw_input STOP ( " termination command is input q to complete the operation: \ n- " )

if stop=="q":

sys.exit()

# break

except Exception as e:

print "good bye"

else:

print s_pos[0][19]

break

except Exception as e:

return e

else:

Print " master from the data does not reach the consistency .......... " , n-

n=0

data=[]

if __name__=="__main__":

= c_time int (the raw_input ( " number of seconds after switch over the primary .. >> " ))

print "****************************************************\n"

Print " Please specify the information prompted, enter: "

= raw_input m_host ( " current main library address: " )

= raw_input m_user ( " current main library Username: " )

= raw_input m_password ( " current main library password: " )

= m_port int (raw_input ( " current main library ports: " ))

print "****************************************************\n"

= raw_input s_host ( " current from the library's address: " )

= raw_input s_user ( " current from Username library: " )

= raw_input s_password ( " current password from the library: " )

= s_port int (raw_input ( " current port from the library: " ))

M = m_s(m_host, m_user, m_password, m_port)

S = m_s(s_host,s_user,s_password,s_port)

= CUR M.getConn () Gets the master database cursors ##

= cur1 S.getConn () ## obtained from the database cursors

print "*****************************************************\n"

Print " master synchronization information from the user ......... \ n- "

= the raw_input s_user1 ( " Enter User >> master-slave synchronization: " )

= the raw_input s_password1 ( " Enter password master-slave synchronization: " )

= s_port1 int (the raw_input ( " input port of master-slave synchronization: " ))

M.execSQLstatus(cur,s_host,s_user1,s_password1,s_port1)

Guess you like

Origin www.cnblogs.com/mysqltongbu/p/11018492.html