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

相关文章

Django1.9 加载通过ImageField上传的图片方法

这里假设你是通过models的ImageField上传图片,并期望在前台img标签中能显示。能否访问图片关键在于,是否能通过正确的路径访问。 在models.py中有image如下...

python用于url解码和中文解析的小脚本(python url decoder)

复制代码 代码如下: # -*- coding: utf8 -*- #! python print(repr("测试报警,xxxx是大猪头".decode("UTF8").encode(...

python基于物品协同过滤算法实现代码

本次测试基于MovieLens数据集实现的基于物品的协同过滤,目前只是在小样本上实现,主要问题是计算太耗内存,后期代码继续优化与完善。 数据集说明:movies.dat中数据是用户对...

python的几种开发工具介绍

1 IDLE1.1 IDLE是python创初人Guido van Rossum使用python and Tkinter来创建的一个集成开发环境。要使用IDLE必须安装python an...

windows系统下Python环境搭建教程

windows系统下Python环境搭建教程

windows系统下Python环境的搭建 step1:下载Python程序 https://www.python.org/downloads/release/python-351/...