可用于监控 mysql Master Slave 状态的python代码
程序员文章站
2024-01-27 08:15:34
复制代码 代码如下:import osimport sysimport mysqldbdef getstatus(conn): quer...
复制代码 代码如下:
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'