如何启用MySQL慢查询日志? [英] How do I enable the MySQL slow query log?
问题描述
我的MySQL版本详细信息是
My MySQL version details are
- 服务器:通过UNIX套接字的Localhost
- 软件:MySQL
- 软件版本:5.0.96-community-log-MySQL Community Edition(GPL)
- 协议版本:10
如何启用MySQL慢查询日志?
How do I enable the MySQL slow query log?
推荐答案
版本5.1.6及更高版本:
Version 5.1.6 and above:
1.输入MySQL Shell并运行以下命令:
set global slow_query_log = 'ON';
2.启用任何其他所需的选项.以下是一些常见的示例:
记录预期将检索所有行而不使用索引的查询的详细信息:
set global log_queries_not_using_indexes = 'ON'
设置慢速查询日志的路径:
set global slow_query_log_file ='/var/log/mysql/slow-query.log';
设置查询在记录之前需要运行的时间:
set global long_query_time = 20;
(default is 10 seconds)
3.通过输入MySQL Shell并运行以下命令,确认更改是活动的:
show variables like '%slow%';
低于5.1.6的版本:
Versions below 5.1.6:
-
使用您喜欢的文本编辑器编辑/etc/my.cnf文件 vi/etc/my.cnf
Edit the /etc/my.cnf file with your favorite text editor vi /etc/my.cnf
在"[mysqld]"部分下添加以下行.随时将日志文件的路径更新为所需的任何内容:
Add the following line under the "[mysqld]" section. Feel free to update the path to the log file to whatever you want:
log-slow-queries=/var/log/mysql/slow-query.log
3.根据需要启用其他选项.以下是上面相同的常用示例:
设置查询在记录之前需要运行的时间:
`long_query_time=20
(default is 10 seconds)`
记录预期将检索所有行而不使用索引的查询的详细信息:
`log-queries-not-using-indexes`
4.重新启动MySQL服务:
service mysqld restart
5.通过输入MySQL Shell并运行以下命令,确认更改是活动的:
show variables like '%slow%';
更新:1
根据MySQL文档,当您对SQLSTATE使用错误的代码时,会发生错误#1193.
According to MySQL docs, the error #1193 occurs when you use wrong code for SQLSTATE.
Message: Unknown system variable %s
而且,正如您在同一页面上看到的那样,未定义SQLSTATE 99003.
And, as you can see on the same page, the SQLSTATE 99003 is not defined.
引用此链接:
http://dev.mysql.com/doc/refman/5.5/en/slow-query-log.html
http://dev.mysql.com/doc/refman/5.1/en/slow-query-log.html
这篇关于如何启用MySQL慢查询日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!