在線上環境中,MySQL 的 Slow Log 經常出現大量類似以下的記錄:
# Time: 130823 13:56:08
# User@Host: repl[repl] @ slave [10.x.x.x]
# Query_time: 9.000833 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 1
SET timestamp=1377237368;
# administrator command: Binlog Dump;
每次 Binlog Dump 都會被記錄,導致 Slow Log 膨脹,干擾真正的慢查詢分析。經排查,這是 MySQL 5.1.x 版本(確認 5.1.49 存在)的已知現象。
修改 my.cnf 配置文件,在 [mysqld] 區塊添加或修改以下選項:
log-slow-admin-statements = 0
注意: 此參數在 MySQL 5.1 中不支持動態修改,必須重啟 mysqld 才能生效。
官方手冊對該參數的描述是:
Include slow administrative statements in the statements written to the slow query log. Administrative statements include ALTER TABLE, ANALYZE TABLE, CHECK TABLE, CREATE INDEX, DROP INDEX, OPTIMIZE TABLE, and REPAIR TABLE.
雖然手冊未明確列出 Binlog Dump,但實測關閉此選項後,Binlog Dump 記錄不再寫入 Slow Log。這說明 Binlog Dump 在內部被歸類為管理命令之一。
若遇到 Slow Log 被大量 Binlog Dump 佔用,設定 log-slow-admin-statements = 0 並重啟服務即可乾淨解決。這也提醒我們:文檔有時不完全,實踐出真知。