如何在MySQL 5.0.45中记录慢速查询

时间:2022-04-22 22:01:53

I am trying to turn on logging of slow queries on MySQL v5.0.45 (compile OS: redhat-linus-gnu), but I can't seem to get it working. I tried adding...

我正在尝试在MySQL v5.0.45 (compile OS: redhat-linus-gnu)上打开慢查询日志记录,但我似乎无法让它正常工作。我试着添加…

long_query_time = 1

to my /etc/my.cnf file as the last option under the [mysqld] section and then I restarted using the command...

将/etc/my.cnf文件作为[mysqld]部分下的最后选项,然后使用命令重新启动……

sudo /sbin/service mysqld restart --log-slow-queries=/var/log/slowqueries.log

Then I ran programs where I know the SELECT query is taking around 6-8 seconds, but I can't find the log anywhere. Any ideas?

然后我运行了一些程序,我知道SELECT查询大约需要6-8秒,但是我在任何地方都找不到日志。什么好主意吗?

2 个解决方案

#1


3  

For MySQL 5.0, the variable you want to use in my.cnf is:

对于MySQL 5.0,您希望在my.cnf中使用的变量是:

log-slow-queries = /var/log/mylogname.log

You will need to create this file manually and set ownership and group to mysql. You can do this on the command line with:

您需要手动创建这个文件,并将所有权和组设置为mysql。您可以在命令行中这样做:

touch /var/log/mylogname.log
chown mysql /var/log/mylogname.log
chgrp mysql /var/log/mylogname.log

You can also set the length of time that you consider "long" with:

你也可以设定你认为“长”的时间长度:

long_query_time = 2

You can add queries that are performed without indexes using:

您可以添加在没有索引的情况下执行的查询:

log-queries-not-using-indexes

But, this might fill up your log really fast.

但是,这会很快填满你的日志。

I found it strange that some variables use underscores and some use hyphens, and it can be different depending on how you set the variables, through my.cnf, or through the command line. Here is a list of all system variables you can set in MySQL 5.0: http://dev.mysql.com/doc/refman/5.0/en/server-options.html

我发现有些变量使用下划线,有些使用连字符,这很奇怪,这取决于如何通过my.cnf来设置变量,或者通过命令行。这里列出了可以在MySQL 5.0中设置的所有系统变量:http://dev.mysql.com/doc/refman/5.0/en/server-options.html。

Something else weird is: this documentation says that log-slow-queries is a Boolean value, and when I check my global variables (in mysql, type 'show global variables;'), it shows the value of this as 'ON', not as the file path I set it to. Regardless, this worked for me, and it wrote the file.

另一件奇怪的事情是:这个文档说log-slow查询是一个布尔值,当我检查全局变量(在mysql中,输入“show global variables;”)时,它显示的值为“ON”,而不是我设置的文件路径。不管怎样,这对我来说是行得通的,而且它写了这个文件。

#2


0  

In your /etc/my.cnf add log_slow_queries = /var/log/mysql/slowqueries.log and then restart mysql as usual, e.g. sudo service mysqld restart and give that a try.

在/etc/my.cnf中添加log_slow_queries = /var/log/mysql/slowqueries。然后像往常一样重新启动mysql,例如sudo服务mysqld重新启动并尝试。

#1


3  

For MySQL 5.0, the variable you want to use in my.cnf is:

对于MySQL 5.0,您希望在my.cnf中使用的变量是:

log-slow-queries = /var/log/mylogname.log

You will need to create this file manually and set ownership and group to mysql. You can do this on the command line with:

您需要手动创建这个文件,并将所有权和组设置为mysql。您可以在命令行中这样做:

touch /var/log/mylogname.log
chown mysql /var/log/mylogname.log
chgrp mysql /var/log/mylogname.log

You can also set the length of time that you consider "long" with:

你也可以设定你认为“长”的时间长度:

long_query_time = 2

You can add queries that are performed without indexes using:

您可以添加在没有索引的情况下执行的查询:

log-queries-not-using-indexes

But, this might fill up your log really fast.

但是,这会很快填满你的日志。

I found it strange that some variables use underscores and some use hyphens, and it can be different depending on how you set the variables, through my.cnf, or through the command line. Here is a list of all system variables you can set in MySQL 5.0: http://dev.mysql.com/doc/refman/5.0/en/server-options.html

我发现有些变量使用下划线,有些使用连字符,这很奇怪,这取决于如何通过my.cnf来设置变量,或者通过命令行。这里列出了可以在MySQL 5.0中设置的所有系统变量:http://dev.mysql.com/doc/refman/5.0/en/server-options.html。

Something else weird is: this documentation says that log-slow-queries is a Boolean value, and when I check my global variables (in mysql, type 'show global variables;'), it shows the value of this as 'ON', not as the file path I set it to. Regardless, this worked for me, and it wrote the file.

另一件奇怪的事情是:这个文档说log-slow查询是一个布尔值,当我检查全局变量(在mysql中,输入“show global variables;”)时,它显示的值为“ON”,而不是我设置的文件路径。不管怎样,这对我来说是行得通的,而且它写了这个文件。

#2


0  

In your /etc/my.cnf add log_slow_queries = /var/log/mysql/slowqueries.log and then restart mysql as usual, e.g. sudo service mysqld restart and give that a try.

在/etc/my.cnf中添加log_slow_queries = /var/log/mysql/slowqueries。然后像往常一样重新启动mysql,例如sudo服务mysqld重新启动并尝试。