Get the App
SLTechnology News&Howtos  ›  Database  › 

Monitor the master-slave status of mysql database

Shulou Source: shulou.com Published: 2022-06-01 06:07:25 10月06日 Update

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

~

Tags: Descendants Gods versions Master and Slave data Database status Monitoring Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Apple Xiaomi NVidia MySQL