Operations 2 min read

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.

Advanced AI Application Practice
Advanced AI Application Practice
Advanced AI Application Practice
Generate a MySQL Monitoring Shell Script with ChatGPT

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.log

Running 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.

databasePerformance MonitoringChatGPTLinuxMySQLshell script
Advanced AI Application Practice
Written by

Advanced AI Application Practice

Advanced AI Application Practice

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.