為什麼MySQL慢查詢日誌不存在?

為什麼MySQL慢查詢日誌不存在?

我今天在我的 CentOS 伺服器上尋找 MySQL 慢查詢日誌,但沒有找到該日誌。我甚至做了“SELECT SLEEP(2)”測試,希望創建這個檔案。請注意,我不太懂伺服器,所以我非常感謝任何幫助。

 7
  8 [mysql.server]
  9 user=mysql
 10 basedir=/var/lib
 11
 12 [mysqld]
 13 long_query_time = 1
 14 log_slow_queries=/var/log/mysql_slow_queries.log
 15 log=/var/log/mysql.query.log
 16
 17 ## FINE TUNING ##
 18
 19 key_buffer              = 16M
 20 max_allowed_packet      = 16M
 21 thread_stack            = 128K
 22 thread_cache_size       = 8
 23 query_cache_limit       = 1M
 24 query_cache_size        = 16M
 25
 26 [mysqld_safe]
 27 log-error=/var/log/mysqld.log
 28 pid-file=/var/run/mysqld/mysqld.pid
 29 safe-show-databases

正如您所看到的,路徑顯示:/var/log/mysql_slow_queries.log——但找不到該檔案。不過,我在目錄中發現 mysql.query.log 和 mysqld.log 很好。為什麼我的 MySQL 慢查詢檔案不存在?

答案1

對檔案進行變更後my.cnf,您需要重新啟動 MySQL 服務。不確定您使用的是什麼系統,但通常service mysqld restart可以工作,或service mysql restart- 只需查看/etc/init.d/.

此外,請確保該檔案已建立並且在磁碟上可用:

touch /var/log/mysql_slow_queries.log
chown mysql:mysql /var/log/mysql_slow_queries.log

此外,時間在資料庫中並不那麼重要,但在沒有索引的查詢中更重要。考慮將以下內容新增至您的my.cnf.當然,一旦完成,請重新啟動MySQL:

log-queries-not-using-indexes

答案2

觸碰/var/log/mysql_slow_queries.log

chown mysql:mysql

並重新啟動你的mysql服務

mysql -u -pmy密碼

選擇睡眠(60);

貓 /var/log/mysql_slow_queries.log

/usr/sbin/mysqld, Version: 5.5.28-log (Source distribution). started with:
Tcp port: 3306  Unix socket: /var/run/mysql/mysql.sock
Time                 Id Command    Argument
# Time: 130904 23:22:14
# User@Host: root[root] @ localhost []
# Query_time: 60.002284  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
SET timestamp=1378329734;
select sleep(60);

答案3

新增slow_query_log=1[mysqld]的部分my.cnf並重新啟動 mysqld。

答案4

請注意,在 Centos 7 中,預設且無需重新啟動 MySQL,慢查詢日誌在此位置啟用:

/var/lib/mysql/$HOSTNAME.log

請參閱其他答案以了解如何套用持久配置以及如何將其移動到另一個位置。

相關內容