mysql延遲判斷模板

來源:互聯網
上載者:User

標籤:自訂模板判斷mysql主從延遲

使用自訂模板

#!/usr/bin/env python#coding:utf-8import MySQLdbimport torndbsql_s = "show slave status"sql_m = "show master status"# 使用MySQLdb的串連def db_conM(args):#     args = {‘host‘:[mhost,shost],‘user‘:user,‘passwd‘:passwd,‘db‘:db,‘port‘:3306,‘con_timeout‘:5}   # 有從庫時,判斷延遲,大於50則串連主庫    if args[‘host‘][1] != ‘‘ :        try:            sdb = MySQLdb.connect(host=args[‘host‘][1],port=args[‘port‘],user=args[‘user‘],passwd=args[‘passwd‘],db=args[‘db‘],charset="utf8",connect_timeout=args[‘con_timeout‘])        except Exception,e:            sdb = MySQLdb.connect(host=args[‘host‘][0],port=args[‘port‘],user=args[‘user‘],passwd=args[‘passwd‘],db=args[‘db‘],charset="utf8",connect_timeout=10)            return sdb                    cursor = sdb.cursor()        cursor.execute(sql_s)        seconds = int(cursor.fetchall()[0][32])        if  seconds > 50 :           sdb = MySQLdb.connect(host=args[‘host‘][0],port=args[‘port‘],user=args[‘user‘],passwd=args[‘passwd‘],db=args[‘db‘],charset="utf8",connect_timeout=10)        return sdb              # 沒有從庫時,直接連接主庫    try:        sdb = MySQLdb.connect(host=args[‘host‘][0],port=args[‘port‘],user=args[‘user‘],passwd=args[‘passwd‘],db=args[‘db‘],charset="utf8",connect_timeout=10)    except Exception,e:        print "Connect failed:%s"%e        return#    cursor = sdb.cursor()    return sdb    # 使用torndb的串連,判斷方法類似def get_conT(args):#    args = {‘host‘:[mhost,shost],‘user‘:user,‘passwd‘:passwd,‘db‘:db,‘port‘:3306,‘con_timeout‘:5}    if args[‘host‘][1] != ‘‘:        try:            sdb = torndb.Connection("%s:%s"%(args[‘host‘][0],3306),args[‘db‘],user=args[‘user‘],password=args[‘passwd‘],time_zone=‘+8:00‘,charset=‘utf8‘,connect_timeout=args[‘con_timeout‘])        except Exception,e:            sdb = torndb.Connection("%s:%s"%(args[‘host‘][1],3306),args[‘db‘],user=args[‘user‘],password=args[‘passwd‘],time_zone=‘+8:00‘,charset=‘utf8‘,connect_timeout=10)            return sdb        seconds = int(sdb.get(sql_s).Seconds_Behind_Master)        if seconds > 50 :            sdb = torndb.Connection("%s:%s"%(args[‘host‘][1],3306),args[‘db‘],user=args[‘user‘],password=args[‘passwd‘],time_zone=‘+8:00‘,charset=‘utf8‘,connect_timeout=10)        return sdb                try:        sdb = torndb.Connection("%s:%s"%(args[‘host‘][1],3306),args[‘db‘],user=args[‘user‘],password=args[‘passwd‘],time_zone=‘+8:00‘,charset=‘utf8‘,connect_timeout=10)    except Exception,e:        print "Connect failed:%s"%e        return    return sdb


將檔案命名成db_help.py,後面的指令碼直接引用,判斷主從,切記在使用完串連後,記得關閉(sdb.close()的關閉)。

或者將以上兩個函數用class進行封裝,同時將sdb.close()函數定義在解構函式中,達到在對象使用完後,自動關閉串連的作用。

mysql延遲判斷模板

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.