可用于监控 mysql Master Slave 状态的python代码
(编辑:jimmy 日期: 2024/11/27 浏览:3 次 )
复制代码 代码如下:
import os
import sys
import MySQLdb
def getStatus(conn):
query = " SHOW SLAVE STATUS "
# print query
cursor = conn.cursor()
cursor.execute(query)
result = cursor.fetchall()
return result[0]
def resolve(conn):
cursor = conn.cursor()
query1 = "set global sql_slave_skip_counter=1"
query2 = "START SLAVE"
query3 = "SHOW SLAVE STATUS"
cursor.execute(query1)
cursor.execute(query2)
cursor.execute(query3)
conn.commit()
if __name__ == '__main__':
conn = MySQLdb.connect(read_default_file="~/.my.cnf", db="", port=3306, charset="utf8")
status = getStatus(conn)
print "Master_Log_File: %s" % status[5]
print "Read_Master_Log_Pos: %s" % status[6]
print "Seconds_Behind_Master: %s" % status[-1]
if status[32] is None:
resolve(conn)
else:
print 'resolved'
import os
import sys
import MySQLdb
def getStatus(conn):
query = " SHOW SLAVE STATUS "
# print query
cursor = conn.cursor()
cursor.execute(query)
result = cursor.fetchall()
return result[0]
def resolve(conn):
cursor = conn.cursor()
query1 = "set global sql_slave_skip_counter=1"
query2 = "START SLAVE"
query3 = "SHOW SLAVE STATUS"
cursor.execute(query1)
cursor.execute(query2)
cursor.execute(query3)
conn.commit()
if __name__ == '__main__':
conn = MySQLdb.connect(read_default_file="~/.my.cnf", db="", port=3306, charset="utf8")
status = getStatus(conn)
print "Master_Log_File: %s" % status[5]
print "Read_Master_Log_Pos: %s" % status[6]
print "Seconds_Behind_Master: %s" % status[-1]
if status[32] is None:
resolve(conn)
else:
print 'resolved'
下一篇:Python高效编程技巧