mysqladmin extended-status等價於 SHOW GLOBAL STATUS,但支援 -r/--relative(差值模式)+ -i/--sleep(輪詢間隔),**極適合寫監控腳本**。
# 每秒輸出一次差值 mysqladmin -uroot -p -r -i 1 extended-status
輸出所有 300+ 狀態變量的**每秒增量**。
mysqladmin -uroot -p -r -i 1 extended-status \ | grep -E "Questions|Queries|Innodb_rows|Com_select|Com_insert|Com_update|Com_delete"
示例輸出:
| Com_delete | 1 | | Com_insert | 321 | | Com_select | 286 | | Com_update | 63 | | Innodb_rows_deleted | 1 | | Innodb_rows_inserted | 207 | | Innodb_rows_read | 5211 | | Innodb_rows_updated | 65 | | Queries | 2721 | | Questions | 2721 |
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
}'
輸出樣例:
<------------- 12:38:49 -------------> Com_delete 0 Com_insert 0 Com_select 0 Com_update 0 Innodb_buffer_pool_read_requests 589 Innodb_rows_deleted 0 Innodb_rows_inserted 2 Innodb_rows_read 50 Innodb_rows_updated 50 Queries 105 Questions 1
mysqladmin -P3306 -uroot -p -h127.0.0.1 -r -i 1 ext \
| awk -F"|" '
BEGIN { count=0 }
{
if ($2 ~ /Variable_name/ && ((++count)%20 == 1)) {
print "----------|---------|--- MySQL Command Status --|----- Innodb row operation ----|-- Buffer Pool Read --"
print "---Time---|---QPS---|select insert update delete| read inserted updated deleted| 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_deleted/) innodb_rows_deleted=$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_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("|%6d %6d %6d %6d", com_select, com_insert, com_update, com_delete)
printf("|%6d %8d %7d %7d", innodb_rows_read, innodb_rows_inserted, innodb_rows_updated, innodb_rows_deleted)
printf("|%10d %11d\n", innodb_lor, innodb_phr)
}
}'
輸出對齊表格:
----------|---------|--- MySQL Command Status --|----- Innodb row operation ----|-- Buffer Pool Read -- ---Time---|---QPS---|select insert update delete| read inserted updated deleted| 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
| 指標 | 含義 | 異常判斷 |
|---|---|---|
Questions | 客戶端發送的查詢數(含預處理) | 驟增 → 應用重試風暴/慢查詢堆積 |
Com_select/insert/update/delete | 各類 DML 執行次數 | 比例失衡 → 寫入熱點/缺少索引 |
Innodb_rows_read/inserted/updated/deleted | 引擎層行操作數 | Read 遠大於 Select → 全表掃描嚴重 |
Innodb_buffer_pool_read_requests | 邏輯讀請求(內存命中) | 配合 Innodb_buffer_pool_reads(物理讀)計算命中率 |
QPS = Questions/sec | 每秒查詢數 | 核心容量指標,結合延遲 P99 判斷飽和度 |
mysqladmin -r -i 1 extended-status + AWK 是**零依賴、輕量級、可嵌入告警系統**的 MySQL 實時監控利器,建議納入日常巡檢與自動化監控體系。