Innotop 是 Perl 編寫的類 top 交互式 MySQL 監控工具,實時展示查詢、事務、InnoDB 狀態、鎖等待、複製延遲等指標,極適合生產排障。
# 1. 依賴 Perl 模塊
cpan Time::HiRes Term::ReadKey DBI DBD::mysql
# 2. 下載源碼
wget https://github.com/innotop/innotop/archive/refs/tags/v1.12.0.tar.gz
tar -zxvf v1.12.0.tar.gz
cd innotop-1.12.0
# 3. 編譯安裝
perl Makefile.PL
make && make install
# 4. 驗證
innotop --help
innotop -u root -p -h localhost -d 3 # -d 刷新間隔秒
進入界面後按 ? 查看快捷鍵:Q Query List、T InnoDB Transactions、L Locks、R Replication、S Slow Queries 等。
SHOW VARIABLES LIKE '%slow%'; / SHOW VARIABLES LIKE '%long%';/etc/my.cnf [mysqld] 區:
slow_query_log = 1
slow_query_log_file = /var/lib/mysql/mysql-slow.log
long_query_time = 2 # 超過 2 秒記錄
log_queries_not_using_indexes = 1 # 可選:記錄無索引查詢
systemctl restart mysqld。SHOW VARIABLES LIKE 'slow_query_log'; -> ON。| 鍵 | 視圖 | 用途 |
|---|---|---|
| Q | Query List | 當前執行查詢,按時間排序,可 Kill |
| T | InnoDB Transactions | 活躍事務、鎖等待、回滾段 |
| L | Locks | 表鎖/行鎖持有者與等待者 |
| S | Slow Queries | 慢查詢日誌實時尾部顯示 |
| R | Replication | 主從延遲、IO/SQL 線程狀態 |
| V | Variables | 運行時參數修改(需 SUPER) |
Innotop 輕量、無依賴、鍵盤驅動,適合 SSH 環境快速定位「當前誰在佔資源、鎖誰、慢查詢長什麼樣」。配合慢查詢日誌,構成「實時+歷史」雙維觀測體系。