可用于监控 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'

相关文章

对Python3 解析html的几种操作方式小结

解析html是爬虫后的重要的一个处理数据的环节。一下记录解析html的几种方式。 先介绍基础的辅助函数,主要用于获取html并输入解析后的结束 #把传递解析函数,便于下面的修改 de...

Python使用Scrapy保存控制台信息到文本解析

Python使用Scrapy保存控制台信息到文本解析

在Windows平台下,如果想运行爬虫的话,就需要在cmd中输入: scrapy crawl spider_name 这时,爬虫就能启动,并在控制台(cmd)中打印一些信息,如下...

利用python求积分的实例

python的numpy库集成了很多的函数。利用其中的函数可以很方便的解决一些数学问题。本篇介绍如何使用python的numpy来求解积分。 代码如下: # -*- coding:...

Python 循环语句之 while,for语句详解

循环语句(有两种): while 语句 for   语句 while 语句: 问题:输入一个整数n,让程序输出n行的: hello 1 hello 2 ....

Django 大文件下载实现过程解析

django提供文件下载时,若果文件较小,解决办法是先将要传送的内容全生成在内存中,然后再一次性传入Response对象中: def simple_file_download(req...