写一段代码 实现 监控mysql数据库的运行状态
时间: 2024-05-13 22:20:17 浏览: 97
MySQL数据库主从复制技术应用实战(阶段二)
Sure, here is a sample code to monitor MySQL database's running status:
```
import mysql.connector
# Create a database connection
cnx = mysql.connector.connect(user='username', password='password',
host='hostname', database='database_name')
# Define the query to check the running status of MySQL
query = "SHOW GLOBAL STATUS LIKE 'Threads_connected'"
# Define a function to execute the query and return the result
def check_mysql_status():
cursor = cnx.cursor()
cursor.execute(query)
result = cursor.fetchone()
cursor.close()
return result[1]
# Call the check_mysql_status() function to get MySQL's running status
mysql_status = check_mysql_status()
# Display the result
print("MySQL is currently connected to " + mysql_status + " threads.")
```
You can run this code periodically to monitor the running status of your MySQL database. Note that you need to replace "username", "password", "hostname", and "database_name" with your own information.
阅读全文