編輯 /etc/my.cnf 中 [mysqld] 區段加入:
slow-query-log=On slow_query_log_file=/data/mysql/log/mysql_slow_query.log long_query_time=2 log_queries_not_using_indexes = ON
slow-query-log=On:啟用慢查詢日誌slow_query_log_file:指定日誌檔案路徑 (需確保目錄存在且 MySQL 進程可寫入)long_query_time=2:超過 2 秒的查詢記入日誌 (可依業務調整,單位秒,支援微秒)log_queries_not_using_indexes=ON:記錄未使用索引的查詢 (即使執行時間短),便於發現潛在效能問題修改後重啟 MySQL 服務生效:systemctl restart mysqld 或 service mysqld restart。
慢查詢日誌是 SQL 調優的核心數據源,建議生產環境常態開啟並定期分析 (可用 mysqldumpslow 或 pt-query-digest 工具)。