可用于监控 mysql Master Slave 状态的python代码

yipeiwu_com5年前Python基础
复制代码 代码如下:

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实现的手机号归属地相关信息查询功能示例

本文实例讲述了Python实现的手机号归属地相关信息查询功能。分享给大家供大家参考,具体如下: 根据指定的手机号码,查询其归属地等相关信息,Python实现: 手机号文件:test.tx...

利用Python实现Windows定时关机功能

利用Python实现Windows定时关机功能

是最初的几个爬虫,让我认识了Python这个新朋友,虽然才刚认识了几天,但感觉有种莫名的默契感。每当在别的地方找不到思路,总能在Python找到解决的办法。自动关机,在平时下载大文件,以...

Python 装饰器@,对函数进行功能扩展操作示例【开闭原则】

本文实例讲述了Python 装饰器@,对函数进行功能扩展操作。分享给大家供大家参考,具体如下: 装饰器可以对原函数进行功能扩展,但还不需要修改原函数的内容(开闭原则),也不需要修改原函数...

python word转pdf代码实例

原理 使用python win32 库 调用word底层vba,将word转成pdf 安装pywin32 pip install pywin32 python代码 fr...

django中瀑布流写法实例代码

django中瀑布流写法实例代码

django中瀑布流初探 img.html <!DOCTYPE html> <html lang="en"> <head> <meta...