mysqladmin extended-status 等價於 SHOW GLOBAL STATUS,但加上 -r (--relative) 參數後,輸出變量的每秒增量 (差值),直接反映當前負載,無需手工計算。
mysqladmin -uroot -p -r -i 1 extended-status
# -r: 顯示差值
# -i 1: 每秒刷新一次
mysqladmin -uroot -p -r -i 1 extended-status \
| grep -E "Questions|Queries|Innodb_rows|Com_select|Com_insert|Com_update|Com_delete"
mysqladmin -uroot -p -h127.0.0.1 -P3306 -r -i 1 ext | \
awk -F"|" '{
if($2 ~ /Variable_name/) {
print " <------------- " strftime("%H:%M:%S") " ------------->";
}
if($2 ~ /Questions|Queries|Innodb_rows|Com_select|Com_insert|Com_update|Com_delete|Innodb_buffer_pool_read_requests/) {
print $2 $3;
}
}'
mysqladmin -P3306 -uroot -p -h127.0.0.1 -r -i 1 ext | \
awk -F"|" '
BEGIN { count=0; }
{
if($2 ~ /Variable_name/ && ++count == 1) {
print "----------|---------|--- MySQL Command Status --|----- Innodb Row Ops ----|-- Buffer Pool Reads --";
print "---Time---|---QPS---|Sel Ins Upd Del|Read Ins Upd Del|Logical Physical";
}
else if ($2 ~ /Queries/) queries=$3;
else if ($2 ~ /Com_select /) com_select=$3;
else if ($2 ~ /Com_insert /) com_insert=$3;
else if ($2 ~ /Com_update /) com_update=$3;
else if ($2 ~ /Com_delete /) com_delete=$3;
else if ($2 ~ /Innodb_rows_read/) innodb_rows_read=$3;
else if ($2 ~ /Innodb_rows_inserted/) innodb_rows_inserted=$3;
else if ($2 ~ /Innodb_rows_updated/) innodb_rows_updated=$3;
else if ($2 ~ /Innodb_rows_deleted/) innodb_rows_deleted=$3;
else if ($2 ~ /Innodb_buffer_pool_read_requests/) innodb_lor=$3;
else if ($2 ~ /Innodb_buffer_pool_reads/) innodb_phr=$3;
else if ($2 ~ /Uptime / && count >= 2) {
printf(" %s |%9d", strftime("%H:%M:%S"), queries);
printf("|%4d %3d %3d %3d", com_select, com_insert, com_update, com_delete);
printf("|%4d %4d %4d %4d", innodb_rows_read, innodb_rows_inserted, innodb_rows_updated, innodb_rows_deleted);
printf("|%8d %9d\n", innodb_lor, innodb_phr);
}
}'
----------|---------|--- MySQL Command Status --|----- Innodb Row Ops ----|-- Buffer Pool Reads --
---Time---|---QPS---|Sel Ins Upd Del|Read Ins Upd Del|Logical Physical
10:37:13 | 2231| 274 214 70 0| 4811 160 71 0| 4146 0
10:37:14 | 2972| 403 256 84 23| 2509 173 85 23| 4545 0
10:37:15 | 2334| 282 232 66 1| 1266 154 67 1| 3543 0
10:37:16 | 2241| 271 217 66 0| 1160 129 66 0| 2935 0
10:37:17 | 2497| 299 224 97 0| 1141 149 95 0| 3831 0
| 指標 | 含義 | 異常閾值參考 |
|---|---|---|
| Queries / Questions | 總查詢數/客戶端發送查詢數 | QPS 突降 > 50% → 疑似阻塞/死鎖 |
| Com_select/insert/update/delete | 各類 DML 執行次數 | Com_update 激增 → 熱點行鎖衝突 |
| Innodb_rows_read/inserted/updated/deleted | 存儲引擎層行操作數 | Rows_read >> Com_select → 全表掃描/索引失效 |
| Innodb_buffer_pool_read_requests | 邏輯讀 (內存命中) | 基準值,配合物理讀算命中率 |
| Innodb_buffer_pool_reads | 物理讀 (磁盤 I/O) | > 0 且持續增長 → Buffer Pool 太小/冷啟動 |
| Buffer Pool 命中率 | 1 - (Physical / Logical) | < 99% 需關注,< 95% 嚴重 |
#!/bin/bash
# Usage: ./mysql_monitor.sh [host] [port] [user] [interval_sec]
HOST=${1:-127.0.0.1}
PORT=${2:-3306}
USER=${3:-root}
INTERVAL=${4:-1}
read -s -p "MySQL Password: " PASS
echo
mysqladmin -h"$HOST" -P"$PORT" -u"$USER" -p"$PASS" -r -i "$INTERVAL" ext | \
awk -F"|" '
BEGIN { c=0; }
{
if($2 ~ /Variable_name/ && ++c == 1) {
print "----------|---------|--- CMD ---|----- ROW OPS -----|-- BUF POOL --";
print "---Time---|---QPS---|Sel Ins Upd Del|Read Ins Upd Del|Logic Phys";
}
else if ($2 ~ /Queries$/) q=$3;
else if ($2 ~ /Com_select /) cs=$3;
else if ($2 ~ /Com_insert /) ci=$3;
else if ($2 ~ /Com_update /) cu=$3;
else if ($2 ~ /Com_delete /) cd=$3;
else if ($2 ~ /Innodb_rows_read$/) irr=$3;
else if ($2 ~ /Innodb_rows_inserted$/) iri=$3;
else if ($2 ~ /Innodb_rows_updated$/) iru=$3;
else if ($2 ~ /Innodb_rows_deleted$/) ird=$3;
else if ($2 ~ /Innodb_buffer_pool_read_requests$/) ior=$3;
else if ($2 ~ /Innodb_buffer_pool_reads$/) ipr=$3;
else if ($2 ~ /Uptime / && c >= 2) {
printf(" %s |%9d", strftime("%H:%M:%S"), q);
printf("|%4d %3d %3d %3d", cs, ci, cu, cd);
printf("|%4d %4d %4d %4d", irr, iri, iru, ird);
printf("|%7d %7d\n", ior, ipr);
}
}'
mysqladmin -r -i 1 是 DBA 排查突發性能問題的「聽診器」:零部署、零侵入、秒級反饋。配合 awk 定製儀表盤,將關鍵指標 (QPS、行操作、緩存命中) 可視化,比 Grafana/Prometheus 更輕量、更即時。建議收藏 Level 4 腳本,故障時一鍵啟動,直擊現場。