使用XAMPP 5.6.21启用mysql慢查询日志

RGr*_*ths 3 mysql xampp error-log mysql-slow-query-log

我看过很多类似的问题,但我似乎找不到答案。我想为我的 MySQL 数据库设置slow query log。我看到很多答案说我应该访问 MySQL 命令行工具。我不确定如何找到这个工具,但我尝试通过以下方式访问它:

c:/xampp/mysql/bin/mysql -u root -p -h localhost

但在这里我得到了 MariaDB,它似乎与我之前见过的任何其他答案/教程不同。输入:

设置 log_slow_queries = ON;

给我错误

错误 1193 (HY000):未知的系统变量“log_slow_queries”

Dre*_*rew 6

SET GLOBAL slow_query_log=1;
Run Code Online (Sandbox Code Playgroud)

慢查询日志包含最多需要几long_query_time秒钟才能完成的查询的日志事件。例如,最多需要 10 秒才能完成。要查看当前设置的时间阈值,请发出以下命令:

SELECT @@long_query_time;
+-------------------+
| @@long_query_time |
+-------------------+
|         10.000000 |
+-------------------+
Run Code Online (Sandbox Code Playgroud)

它可以设置为 GLOBAL 变量,在my.cnfmy.ini文件中。或者可以通过连接来设置,尽管这种情况不常见。该值可设置在 0 到 10(秒)之间。使用什么价值?

  • 10太高了,几乎没用;
  • 2是妥协;
  • 0.5 和其他分数也是可能的;
  • 0 捕获一切;这可能会快速填满磁盘,非常危险,但非常有用。

慢速查询的捕获可以打开或关闭。并且还指定了记录到的文件。下面捕获了这些概念:

SELECT @@slow_query_log; -- Is capture currently active? (1=On, 0=Off)
SELECT @@slow_query_log_file; -- filename for capture. Resides in datadir
SELECT @@datadir; -- to see current value of the location for capture file

SET GLOBAL slow_query_log=0; -- Turn Off
-- make a backup of the Slow Query Log capture file. Then delete it.
SET GLOBAL slow_query_log=1; -- Turn it back On (new empty file is created)
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅 MySQL 手册页慢查询日志

注意:以上关于打开/关闭slowlog的信息在5.6(?)中已更改;旧版本有另一种机制。

查看是什么导致系统变慢的“最佳”方法:

long_query_time=...
turn on the slowlog
run for a few hours
turn off the slowlog (or raise the cutoff)
run pt-query-digest to find the 'worst' couple of queries.  Or mysqldumpslow -s t
Run Code Online (Sandbox Code Playgroud)