| 變數 | 含義 | 異常判讀 |
|---|---|---|
Aborted_clients | 客戶端異常斷開數 | 驟增 → 檢查網路/防火牆/應用連線池配置 |
Aborted_connects | 連線嘗試失敗數 | 驟增 → 檢查認證錯誤、max_connections 滿、主機拒絕 |
Connections | 總連線嘗試次數 | 配合 Threads_created 計算緩存命中率 |
Created_tmp_tables | 隱含臨時表建立數 | 過大 → 檢查查詢優化、tmp_table_size |
Delayed_insert_threads | DELAYED 插入處理緒數 | -- |
Delayed_writes | DELAYED 寫入行數 | -- |
Delayed_errors | DELAYED 寫入錯誤行數 | > 0 → 檢查唯一鍵衝突等 |
Flush_commands | FLUSH 指令執行次數 | -- |
Handler_read_first | 讀索引首行次數 | 高 → 全索引掃描多 |
Handler_read_key | 索引鍵值讀取次數 | 高 → 索引查詢多 (正常) |
Handler_read_next | 依索引序讀下一行 | 高 → 範圍查詢多 |
Handler_read_rnd | 固定位置讀取 (需排序) | 高 → 大量排序/全表掃描 |
Handler_read_rnd_next | 讀下一行 (全表掃描) | 高 → 缺索引、需優化 |
Key_read_requests | 索引快取讀取請求 | 配合 Key_reads 算命中率 |
Key_reads | 實體磁碟讀索引塊 | 高 → key_buffer_size 不足 |
Max_used_connections | 歷史峰值併發連線 | 規劃 max_connections 依據 |
Open_tables | 目前開啟表數 | 配合 Opened_tables 算快取命中 |
Opened_tables | 歷史開啟表總數 | 快速增長 → table_open_cache 過小 |
Questions | 總查詢數 (含所有類型) | QPS 基準 = Questions / Uptime |
Slow_queries | 慢查詢累計數 | > 0 需分析慢查詢日誌 |
Threads_connected | 當前開啟連線數 | 即時併發連線數 |
Threads_running | 非睡眠執行緒數 | 即時活躍查詢數 |
Uptime | 服務運行秒數 | 重啟後歸零 |
# 单次快照
mysqladmin -u user -p -h host extended-status
# 类 top 实时刷新 (每秒刷新, 显示 100 行)
mysqladmin -u user -p -h host extended-status -i 1 -c 100 -r
# 抓取特定指标
mysqladmin ext -i 1 | grep -i 'threads\|questions\|slow'
collect_mysql.sh) 每分鐘跑一次:#!/bin/bash
HOST="127.0.0.1"
USER="monitor"
PASS="xxx"
RRD="/var/lib/rrd/mysql.rrd"
VALUES=$(mysqladmin -u$USER -p$PASS -h$HOST extended-status -r | \
awk '/^(Threads_connected|Threads_running|Questions|Slow_queries|Key_read_requests|Key_reads|Open_tables|Opened_tables|Created_tmp_disk_tables|Created_tmp_tables|Table_locks_waited|Table_locks_immediate)\|/ {print $4}' | \
paste -sd:)
rrdtool update $RRD N:$VALUES
rrdtool create mysql.rrd --step 60 \
DS:threads_c:GAUGE:120:0:10000 \
DS:threads_r:GAUGE:120:0:10000 \
DS:questions:DERIVE:120:0:1000000 \
DS:slow_q:DERIVE:120:0:10000 \
DS:key_req:DERIVE:120:0:10000000 \
DS:key_read:DERIVE:120:0:1000000 \
DS:open_t:GAUGE:120:0:100000 \
DS:opened_t:DERIVE:120:0:1000000 \
DS:tmp_disk:DERIVE:120:0:100000 \
DS:tmp_mem:DERIVE:120:0:1000000 \
DS:lock_wait:DERIVE:120:0:1000000 \
DS:lock_imm:DERIVE:120:0:1000000 \
RRA:AVERAGE:0.5:1:525600 \
RRA:AVERAGE:0.5:60:8760 \
RRA:MAX:0.5:1:525600
Threads_connected / max_connections > 0.85 → 告警Slow_queries 5 分鐘增量 > 10 → 告警Key_reads / Key_read_requests > 0.01 → 告警Created_tmp_disk_tables / Created_tmp_tables > 0.25 → 告警Table_locks_waited / Table_locks_immediate > 0.001 → 告警Uptime: 123456
Threads: 15
Questions: 987654
Slow queries: 12
Opens: 3456
Flush tables: 1
Open tables: 256
Memory in use: 123M
快速掌握服務健康度的單行摘要。