Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Monitor the master-slave status of mysql database

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Python version 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 ()

Listen to the revised code of the Great God:

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

~

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report