tuning-primer.sh 是 Matthew Montgomery 開發的 MySQL 效能診斷腳本,能自動採集關鍵狀態變數、計算健康度指標、輸出彩色分級建議。本文詳細解讀實戰報告各區塊含義,並提供參數調優決策樹,助您從「看得懂報告」進化到「會調優參數」。
wget https://launchpad.net/mysql-tuning-primer/trunk/1.6-r1/+download/tuning-primer.sh chmod +x tuning-primer.sh ./tuning-primer.sh
前提: MySQL 運行 ≥ 48 小時(確保狀態變數具代表性),具備 PROCESS、SUPER 權限帳號。
| 顏色 | 等級 | 含義 | 行動 |
|---|---|---|---|
| 藍色 | 標題 | 區塊標題、參考連結 | 閱讀 |
| 綠色 | 正常 | 參數合理、指標健康 | 維持 |
| 黃色 | 提示 | 潛在風險、建議關注 | 評估 |
| 紅色 | 警告 | 明顯亞優、需調整 | 計劃調優 |
| 深紅 | 嚴重 | 關鍵錯誤、立即處理 | 緊急修復 |
The slow query log is NOT enabled. Current long_query_time = 10.000000 sec. You have 9589 out of 88956228 that take longer than 10.000000 sec. to complete
診斷: 慢查詢日誌未開啟,但估算有 9.5 萬筆查詢超過 10 秒。
處方: 立即開啟慢查詢日誌,降低 long_query_time 至 2-5 秒,啟用 log_queries_not_using_indexes。
The binary update log is enabled The expire_logs_days is not set. The mysqld will retain the entire binary log until RESET MASTER or PURGE MASTER LOGS commands are run manually
風險: Binlog 無限增長佔滿磁碟。
處方: expire_logs_days = 7(配合備份週期),或啟用 binlog_expire_logs_seconds (MySQL 8.0+)。
Max Memory Ever Allocated : 11.87 G
Configured Max Per-thread Buffers : 10.38 G
Configured Max Global Buffers : 6.10 G
Configured Max Memory Limit : 16.48 G
Physical Memory : 8.00 G
Max memory limit exceeds 90% of physical memory
根因: 理論最大記憶體 (Global + Per-thread × Max Connections) = 16.48 GB,遠超實體 8 GB。
處方:
max_connections(如 151 → 100)或升級實體記憶體sort_buffer_size、join_buffer_size、read_buffer_size、read_rnd_buffer_size、thread_stackinnodb_buffer_pool_size ≈ 實體記憶體 60-70%(扣除系統、線程、其他緩衝)Current MyISAM index space = 8 K
Current key_buffer_size = 2.00 G
Key cache miss rate is 1 : 585673
Key buffer free ratio = 0 %
Your key_buffer_size seems to be too high. Perhaps you can use these resources elsewhere
診斷: 實際 MyISAM 索引僅 8 KB,卻分配 2 GB,命中率極低(1:58萬),空閒率 0%。
處方: key_buffer_size = 64M(或根據實際索引大小 + 20% 預留),釋放記憶體給 InnoDB Buffer Pool。
Current join_buffer_size = 132.00 K
You have had 1 queries where a join could not use an index properly
You should enable "log-queries-not-using-indexes"
Note! This script will still suggest raising the join_buffer_size when ANY joins not using indexes are found.
診斷: 存在非索引 Join,腳本建議加大 join_buffer_size,但治本在於加索引,而非加 Buffer。
處方: 開啟 log_queries_not_using_indexes,定位非索引 Join SQL,補齊索引;join_buffer_size 維持 256K-1M 即可。
Current read_buffer_size = 2 M
Current table scan ratio = 4650 : 1
You have a high ratio of sequential access requests to SELECTs. You may benefit from raising read_buffer_size and/or improving your use of indexes.
診斷: 每 4650 次 SELECT 就有一次全表掃描。
處方: 優先補索引消除全表掃描;read_buffer_size 可調至 4-8M 緩解剩餘掃描壓力。
實體記憶體 8GB ├─ 系統保留: 1 GB ├─ InnoDB Buffer Pool: 4.5 GB (≈ 60%) ├─ 線程私有 (max_connections=200): │ ├─ sort_buffer_size: 2 MB │ ├─ join_buffer_size: 512 KB │ ├─ read_buffer_size: 2 MB │ ├─ read_rnd_buffer_size: 1 MB │ └─ thread_stack: 256 KB │ 小計: 200 × 5.75 MB ≈ 1.15 GB ├─ MyISAM Key Buffer: 64 MB (實際索引極小) ├─ Query Cache: 0 (禁用) ├─ Binlog Cache: 32 MB └─ 其他/預留: ~1.2 GB 總計 ≈ 7.9 GB < 8 GB ✓
Max_used_connections、Threads_connected、OOM 風險Innodb_buffer_pool_reads、Innodb_log_waits、Created_tmp_disk_tablesTable_locks_waited、Innodb_row_lock_waits、Innodb_row_lock_time_avgKey_read_requests / Key_reads、Qcache_hits / Qcache_insertsinnodb_dedicated_server、innodb_buffer_pool_chunk_size、performance_schema 記憶體)無感知。建議搭配 MySQLTuner.pl、Percona Toolkit pt-variable-advisor、Prometheus + Grafana 監控體系綜合診斷。
從「看報告」到「懂根因、會決策、建閉環」,tuning-primer 是入門利器,但真正的調優功力在於對 MySQL 內核機制的深度理解與實戰驗證循環。