如何在不重新启动 MySQL 的情况下启用 MySQL 的慢查询日志?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2403793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I enable MySQL's slow query log without restarting MySQL?
提问by mmattax
I followed the instructions here: http://crazytoon.com/2007/07/23/mysql-changing-runtime-variables-with-out-restarting-mysql-server/but that seems to only set the threshold.
我按照这里的说明进行操作:http: //crazytoon.com/2007/07/23/mysql-changed-runtime-variables-with-out-restarting-mysql-server/但这似乎只是设置了阈值。
Do I need to do anything else like set the filepath?
我是否需要执行其他操作,例如设置文件路径?
According to MySQL's docs
根据 MySQL 的文档
If no file_name value is given for --log-slow-queries, the default name is host_name-slow.log. The server creates the file in the data directory unless an absolute path name is given to specify a different directory.
Running
跑步
SHOW VARIABLES
doesn't indicate any log file path and I don't see any slow query log file on my server...
不指示任何日志文件路径,我在我的服务器上没有看到任何慢查询日志文件...
EDIT
编辑
Looks like I'm using server version 5.0.77, so I needed to do:
看起来我使用的是服务器版本 5.0.77,所以我需要做:
SET GLOBAL log_slow_queries = 1;
but I get: ERROR 1238 (HY000): Variable 'log_slow_queries' is a read only variable
但我得到:ERROR 1238 (HY000): 变量“log_slow_queries”是只读变量
I assume I'm going to need to restart the server and have log_slow_queries set in my config?
我假设我需要重新启动服务器并在我的配置中设置 log_slow_queries?
回答by Ian Gregory
Try SET GLOBAL slow_query_log = 'ON';
and perhaps FLUSH LOGS;
尝试SET GLOBAL slow_query_log = 'ON';
,也许FLUSH LOGS;
This assumes you are using MySQL 5.1 or later. If you are using an earlier version, you'll need to restart the server. This is documented in the MySQL Manual. You can configure the log either in the config file or on the command line.
这假设您使用的是 MySQL 5.1 或更高版本。如果您使用的是早期版本,则需要重新启动服务器。这在MySQL 手册中有记录。您可以在配置文件或命令行中配置日志。
回答by Nitin
For slow queries on version < 5.1, the following configuration worked for me:
对于版本 < 5.1 的慢查询,以下配置对我有用:
log_slow_queries=/var/log/mysql/slow-query.log
long_query_time=20
log_queries_not_using_indexes=YES
Also note to place it under [mysqld]
part of the config file and restart mysqld
.
另请注意将其放在[mysqld]
配置文件的一部分下并重新启动mysqld
。
回答by Sivaprabu Ganesan
Find log enabled or not?
查找日志是否启用?
SHOW VARIABLES LIKE '%log%';
Set the logs:-
设置日志:-
SET GLOBAL general_log = 'ON';
SET GLOBAL slow_query_log = 'ON';
回答by Martin
MySQL Manual - slow-query-log-file
This claims that you can run the following to set the slow-log file (5.1.6 onwards):
这声称您可以运行以下命令来设置慢日志文件(5.1.6 以上):
set global slow_query_log_file = 'path';
The variable slow_query_log just controls whether it is enabled or not.
变量 slow_query_log 只控制是否启用它。
回答by mikeytown2
These work
这些工作
SET GLOBAL LOG_SLOW_TIME = 1;
SET GLOBAL LOG_QUERIES_NOT_USING_INDEXES = ON;
Broken on my setup 5.1.42
在我的设置 5.1.42 上损坏
SET GLOBAL LOG_SLOW_QUERIES = ON;
SET GLOBAL SLOW_QUERY_LOG = ON;
set @@global.log_slow_queries=1;
http://bugs.mysql.com/bug.php?id=32565
http://bugs.mysql.com/bug.php?id=32565
Looks like the best way to do this is set log_slow_time very high thus "turning off" the slow query log. Lower log_slow_time to enable it. Use the same trick (set to OFF) for log_queries_not_using_indexes.
看起来最好的方法是将 log_slow_time 设置得非常高,从而“关闭”慢查询日志。降低 log_slow_time 以启用它。对 log_queries_not_using_indexes 使用相同的技巧(设置为 OFF)。
回答by Jonathan
I think the problem is making sure that MySQL server has the rights to the file and can edit it.
我认为问题在于确保 MySQL 服务器有权访问该文件并可以对其进行编辑。
If you can get it to have access to the file, then you can try setting:SET GLOBAL slow_query_log = 1;
如果您可以让它访问该文件,那么您可以尝试设置:SET GLOBAL slow_query_log = 1;
If not, you can always 'reload' the server after changing the configuration file. On linux its usually /etc/init.d/mysql reload
如果没有,您可以在更改配置文件后始终“重新加载”服务器。在 linux 上它通常/etc/init.d/mysql reload
回答by dilraj singh
If you want to enable general error logs and slow query error log in the table instead of file
如果要在表而不是文件中启用一般错误日志和慢查询错误日志
To start logging in table instead of file:
要开始登录表而不是文件:
set global log_output = “TABLE”;
To enable general and slow query log:
要启用常规和慢速查询日志:
set global general_log = 1;
set global slow_query_log = 1;
To view the logs:
查看日志:
select * from mysql.slow_log;
select * from mysql.general_log;
For more details visit this link
有关更多详细信息,请访问此链接
回答by gerard
This should work on mysql > 5.5
这应该适用于 mysql > 5.5
SHOW VARIABLES LIKE '%long%';
SET GLOBAL long_query_time = 1;
显示变量如 '%long%';
设置全局 long_query_time = 1;