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

yipeiwu_com6年前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'

相关文章

django定期执行任务(实例讲解)

要在django项目中定期执行任务,比如每天一定的时间点抓取数据,刷新数据库等,可以参考stackoverflow的方法,先编写一个manage.py命令,然后使用crontab来定时执...

Python基于PycURL实现POST的方法

本文实例讲述了Python基于PycURL实现POST的方法。分享给大家供大家参考。具体如下: import pycurl import StringIO import urllib...

python中的decorator的作用详解

1、概念 装饰器(decorator)就是:定义了一个函数,想在运行时动态增加功能,又不想改动函数本身的代码。可以起到复用代码的功能,避免每个函数重复性编写代码,简言之就是拓展原来函数功...

python 将字符串完成特定的向右移动方法

# 将字符串中的元素完成特定的向右移动,参数:字符串、移动长度 如:abcdef,移动2,结果:efabcd #原始方法,基本思想:末尾元素移动到开头,其他的元素依次向后移动.代码如下:...

Python2和Python3.6环境解决共存问题

Linux下安装Python3.6和第三方库 /post/150478.htm 如果本机安装了python2,尽量不要管他,使用python3运行python脚本就好,因为可能有程序依...