温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

监控mysql数据库主从状态

发布时间:2020-08-10 15:15:03 来源:网络 阅读:1393 作者:妙曼 栏目:数据库

python版本2.6.6


#!/usr/bin/python

import MySQLdb

def check_mysql():
    status = True
    try:
        conn=MySQLdb.connect(host='172.7.10.8',user='summer',passwd='summer',db='cms')
        cur=conn.cursor()
        cur.execute("show slave status;")
        result = cur.fetchall()
        io_thread=result[0][10]
        sql_thread=result[0][11]
        #print io_thread,sql_thread
        cur.close()
        conn.close()
        status = True
        if io_thread == "Yes" and sql_thread == "Yes":
            print '2'
            status = True
        else:
            print '0'
            status = False
    except Exception,e:
        print Exception,":",e

go = check_mysql()


听大神的修改后代码:





#!/usr/bin/python

import MySQLdb

def check_mysql():
    try:
         conn=MySQLdb.connect(host='172.7.10.8',user='summer',passwd='summer',

        db='cms')

        cur=conn.cursor(MySQLdb.cursors.DictCursor)
        cur.execute('show slave status')
        result=cur.fetchall()
        if result is None:
            raise EnvironmentError('result is None')
        slave_status=result[0]
        io_thread=slave_status.get('Slave_IO_Running')
        sql_thread=slave_status.get('Slave_SQL_Running')
        if io_thread == "Yes" and sql_thread == "Yes":
            print 'it is status ok'
        else:
            print 'io_status is %s \nsql_status is %s' %(io_thread,sql_thread)

    finally:
        cur.close()
        conn.close()

if __name__ == '__main__':
    check_mysql()
~

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI