监控mysql数据库主从状态-创新互联

python版本2.6.6

成都创新互联公司自2013年创立以来,是专业互联网技术服务公司,拥有项目网站设计制作、成都网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元尤溪做网站,已为上家服务,为尤溪各地企业和个人服务,联系电话:13518219792

#!/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()
~

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页名称:监控mysql数据库主从状态-创新互联
文章起源:http://pwwzsj.com/article/cdsohi.html