Python monitors mysql max connections

Source: Internet
Author: User

#!/usr/local/bin/python3.5
Import Pymysql
Import time

sum = 0

While True:
#open DB connection
db = Pymysql.connect (' localhost ', ' root ', ' abc-123 ', ' test ')

#use cursor ()
cursor = Db.cursor ()

#use execute () Run SQL
Cursor.execute ("Show variables like '%max_connections% ';")

#USE Fetchone ()
Max = Cursor.fetchone ()

Cursor.execute ("Show global Status Like ' Max_used_connections ';")
History_max = Cursor.fetchone ()

Cursor.execute ("Show global Status Like ' threads_connected ';")
currently = Cursor.fetchone ()

sum = sum + 1
Print ("\ n--------------------------------\ n")
Print ("Statistic Date:", Time.strftime ('%y-%m-%d%h:%m:%s '))
Print ("Current count:", sum)
Print ("MySQL max connections:", max[1])
Print ("MySQL History max connections:", history_max[1])
Print ("MySQL Current max connections:", currently[1])

Db.close ()
Time.sleep (5)

Output Result:

[Email protected] jb]#./mysql.py

--------------------------------

Statistics Date: 2016-05-25 09:31:52
Current number of statistics: 1
MySQL Maximum number of connections: 2000
MySQL history maximum number of connections: 6
Current maximum number of MySQL connections: 1

--------------------------------

Statistics Date: 2016-05-25 09:31:57
Current number of statistics: 2
MySQL Maximum number of connections: 2000
MySQL history maximum number of connections: 6
Current maximum number of MySQL connections: 1

--------------------------------

Statistics Date: 2016-05-25 09:32:02
Current number of statistics: 3
MySQL Maximum number of connections: 2000
MySQL history maximum number of connections: 6
Current maximum number of MySQL connections: 1

END!

Python monitors mysql max connections

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.