Using ChatGPT to Generate a Shell Script for MySQL Performance Monitoring
This article shares a concise 10‑line Bash script, generated by ChatGPT, that captures MySQL QPS, TPS, and connection counts, timestamps each record, and appends the metrics to a log file for easy performance monitoring.
Inspired by a Zhihu question, the author asked ChatGPT to produce a short shell script for monitoring MySQL database performance. ChatGPT returned a 10‑line Bash script that gathers key metrics and writes them to a 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.logThe script uses mysqladmin to query the server status, extracts the fifth, eighth, and fourth fields for QPS, TPS, and connections respectively, and records them together with a Unix timestamp. The output is appended to /var/log/mysql_perf.log, creating a simple time‑series log of performance data.
The author notes that such a script is convenient for technical users and suggests trying it out.
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.
