慢查詢日誌能夠幫助DBA或開發人員定位可能存在問題的SQL語句,從而進行優化。mysql
默認狀況下,MySQL是不開啓慢查詢日誌的。能夠經過如下命令查看是否開啓:sql
mysql> SHOW VARIABLES LIKE 'long_query_time'; +-----------------+-----------+ | Variable_name | Value | +-----------------+-----------+ | long_query_time | 10.000000 | +-----------------+-----------+ 1 row in set (0.01 sec) mysql> SHOW VARIABLES LIKE 'slow_query_log'; +----------------+-------+ | Variable_name | Value | +----------------+-------+ | slow_query_log | OFF | +----------------+-------+ 1 row in set (0.00 sec)
long_query_time
查詢超過多少秒才記錄。slow_query_log
慢查詢開啓狀態。經過如下命令能夠在客戶端臨時開啓慢查詢日誌:優化
mysql> set global slow_query_log='ON'; Query OK, 0 rows affected (0.00 sec) mysql> set global slow_query_log_file='/var/lib/mysql/instance-1-slow.log'; Query OK, 0 rows affected (0.00 sec) mysql> set global long_query_time=2; Query OK, 0 rows affected (0.00 sec)
也能夠在服務端配置 mysql ,經過修改配置文件的這幾個參數來開啓:日誌
/etc/my.cnf [mysqld] slow_query_log = ON slow_query_log_file = /var/lib/mysql/instance-1-slow.log long_query_time = 2
配置後,重啓 mysql 服務便可。code