Generate a MySQL Monitoring Shell Script with ChatGPT
This article shows how a concise 10‑line shell script, created with ChatGPT, can continuously capture MySQL QPS, TPS, and connection counts and append the metrics to a log file for performance monitoring.
A user on Zhihu asked for a simple way to monitor MySQL performance. Using ChatGPT, a 10‑line Bash script was produced that retrieves the current timestamp, queries MySQL for QPS, TPS, and active connections via mysqladmin, and writes the comma‑separated values to /var/log/mysql_perf.log.
The script works as follows:
#!/bin/bash
# Get current timestamp
TIMESTAMP=`date +%s`
# Get MySQL QPS, TPS, and connection count
QPS=`mysqladmin -uroot -ppassword status | awk '{print $5}'`
TPS=`mysqladmin -uroot -ppassword status | awk '{print $8}'`
CONNECTIONS=`mysqladmin -uroot -ppassword status | awk '{print $4}'`
# Append results to log file
echo "$TIMESTAMP,$QPS,$TPS,$CONNECTIONS" >> /var/log/mysql_perf.logRunning this script at regular intervals (e.g., via cron) provides a time‑series log of key performance indicators, enabling quick detection of load spikes or connection issues.
The article also includes a link to the GPT‑3.5 interface (https://cmqc.ink/) that was used to generate the script, and an illustrative image of the script output.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
