mysqltuner.pl 是 Perl 編寫的 MySQL 性能自動掃描腳本,能快速輸出配置合理性評估、內存使用風險、緩存命中率、鎖爭用等關鍵指標,並給出調優建議。本文詳述安裝、運行、報告解讀與參數調優實戰。
安裝與運行
# 1. 下載(GitHub 最新版)
wget https://raw.githubusercontent.com/major/MySQLTuner-perl/master/mysqltuner.pl
# 或 git clone https://github.com/major/MySQLTuner-perl.git
# 2. 賦予執行權限
chmod +x mysqltuner.pl
# 3. 運行(需 Perl 環境,系統通常預裝)
./mysqltuner.pl
# 或指定登錄參數
./mysqltuner.pl --user root --pass 'password' --host 127.0.0.1 --port 3306
前置要求:MySQL 運行超 24 小時(樣本足夠)、擁有 PROCESS/SELECT 權限的監控賬號。
報告核心板塊解讀
1. General Statistics(基礎狀態)
[OK] Currently running supported MySQL version:版本在支持週期內。
[!!] Total fragmented tables: 29:29 張表存在碎片,需 OPTIMIZE TABLE。
2. Storage Engine Statistics(引擎分布)
- 統計各引擎數據量:
Data in MyISAM tables: 889M (Tables: 479)。
- 關注:是否有未預期的引擎(如仍在用 MyISAM 核心業務表)。
3. Security Recommendations(安全)
[OK] All database users have passwords assigned:無空密碼賬號。
4. Performance Metrics(性能核心指標 —— 重點)
| 指標 | 示例值 | 健康標準 | 異常含義 |
| Maximum possible memory usage | 3.5G (175% RAM) | < 80-90% 物理內存 | 嚴重超配,風險 OOM Killer 殺進程。需降低 buffer 或加內存 |
| Slow queries | 0% (48K/4M) | < 5-10% | 慢查詢佔比高,需開啟慢查詢日誌分析 |
| Key buffer hit rate | 100.0% (2B/205K) | > 99% | MyISAM 索引緩存命中率低,增大 key_buffer_size |
| Query cache efficiency | 49.3% (1M/3M) | > 50-60% | 查詢緩存命中率低,考慮增大 query_cache_size 或關閉(高併發寫入場景) |
| Query cache prunes per day | 268,042 | 低 | 頻繁剔舊,query_cache_size 太小或碎片化嚴重 |
| Table cache hit rate | 18% (256/1K) | > 80% | 表緩存不足,增大 table_open_cache (舊版 table_cache) |
| Table locks acquired immediately | 88% | > 95% | 鎖等待較多,優化查詢/遷移 InnoDB 行鎖 |
5. Recommendations(建議彙總)
General recommendations:
Run OPTIMIZE TABLE to defragment tables for better performance
MySQL started within last 24 hours - recommendations may be inaccurate
Reduce your overall MySQL memory footprint for system stability
Increase table_cache gradually to avoid file descriptor limits
Optimize queries and/or use InnoDB to reduce lock wait
Variables to adjust:
*** MySQL's maximum memory usage is dangerously high ***
*** Add RAM before increasing MySQL buffer variables ***
query_cache_size (> 32M)
table_cache (> 256)
實戰調優動作清單
- 修正內存超配:
innodb_buffer_pool_size 為最大塊頭。若 16GB 內存,設 10-12GB;key_buffer_size(MyISAM)設 256M-512M;query_cache_size 設 32-64M(或關閉 query_cache_type=0)。
- 擴大表緩存:
table_open_cache = 2048(配合 open_files_limit=65535)。
- 碎片整理:
OPTIMIZE TABLE tbl1, tbl2, ...(僅 MyISAM/InnoDB 觸發 rebuild,大表謹慎、低峰期執行)。
- 查詢緩存決策:高併發寫入(> 20% 寫入)建議關閉查詢緩存(
query_cache_type=0, query_cache_size=0),鎖爭用收益大於命中收益。
- 持久化配置:將調整寫入
/etc/my.cnf [mysqld],systemctl restart mysqld 生效。
進階:定期自動化巡檢
# crontab -e
0 3 * * 1 /usr/local/bin/mysqltuner.pl --user monitor --pass 'xxx' --host 127.0.0.1 --nobad --noinfo >> /var/log/mysqltuner.log 2>&1
# --nobad --noinfo 只輸出警告/建議,便於告警腳本解析
局限性提示
- 基於當前狀態快照的啟發式規則,非成本模型優化器,建議具備參考性,不可盲目執行。
- 無法感知業務查詢模式、數據分布、索引設計缺陷,需結合
EXPLAIN/慢查詢日誌/pmm/gtid 等工具綜合判斷。
- MySQL 8.0 移除 Query Cache,報告中相關指標將消失/歸零。
總結
mysqltuner.pl 是 MySQL DBA/開發者的「體檢儀」:5 分鐘掃描,輸出內存風險、緩存命中、鎖爭用、碎片等核心指標與調優建議。核心價值在於快速定位「內存超配」「表緩存不足」「查詢緩存失效」三大高頻隱患。建議納入週度巡檢體系,配合慢查詢分析、索引審計,構建全鏈路性能治理閉環。
- 上一篇文章: MySQL 連接查詢排序優化:先排序後關聯,效能提升 20 倍實戰
- 下一篇文章: MySQL 主鍵與索引關系深度解析:邏輯鍵 vs 物理鍵、唯一性約束與查詢加速
Copyright ©
程式碼之家 All Rights Reserved