MySQL如何开启记录从客户端收到的每条SQL查询语句和提交时间的功能? 我可以在phpmyadmin或NaviCat中这样做吗? 如何分析日志?


当前回答

这不是问题的确切答案,因为这个问题已经有了很好的答案。这是附带信息。启用general_log确实降低了MySQL的性能。我意外地将general_log =1留在了一台生产服务器上,并花了几个小时来找出为什么性能不能与其他服务器上的类似设置相比。然后我发现这解释了启用常规日志的影响。http://www.fromdual.com/general_query_log_vs_mysql_performance。

要点在于,不要将general_log=1放在.cnf文件中。相反,在短时间内使用set global general_log =1来记录足够多的日志,以便找到您想要查找的内容,然后将其关闭。

其他回答

你可能会遇到一组十六进制值,像这样(参数列):

mysql> select * from mysql.general_log LIMIT 1\G
*************************** 1. row ***************************
  event_time: 2023-01-27 13:37:20.950778
   user_host: root[root] @ localhost []
   thread_id: 1434
   server_id: 1
command_type: Query
    argument: 0x73656C656374202A2066726F6D207573657273
1 row in set (0.00 sec)

所以为了让它更具可读性,只需使用:

select a.*, convert(a.argument using utf8) from mysql.general_log a;

返回值是这样的:

mysql> select a.*, convert(a.argument using utf8) from mysql.general_log a LIMIT 1\G
*************************** 1. row ***************************
                    event_time: 2023-01-27 13:37:20.950778
                     user_host: root[root] @ localhost []
                     thread_id: 1434
                     server_id: 1
                  command_type: Query
                      argument: 0x73656C656374202A2066726F6D207573657273
convert(a.argument using utf8): select * from users
1 row in set, 1 warning (0.00 sec)

Ps:我在例子中使用了LIMIT 1,因为我的日志表太大了。

MySQL 5.6版本有bug。 甚至mysqld显示为:

    Default options are read from the following files in the given order:
C:\Windows\my.ini C:\Windows\my.cnf C:\my.ini C:\my.cnf c:\Program Files (x86)\MySQL\MySQL Server 5.6\my.ini c:\Program Files (x86)\MySQL\MySQL Server 5.6\my.cnf 

实际设置按以下顺序读取:

    Default options are read from the following files in the given order:
C:\ProgramData\MySQL\MySQL Server 5.6\my.ini C:\Windows\my.ini C:\Windows\my.cnf C:\my.ini C:\my.cnf c:\Program Files (x86)\MySQL\MySQL Server 5.6\my.ini c:\Program Files (x86)\MySQL\MySQL Server 5.6\my.cnf

检查文件:C:\ProgramData\MySQL\MySQL Server 5.6\my.ini

希望它能帮助到一些人。

当我想快速优化不同的页面加载时,我使用这种方法进行日志记录。 这是一个小建议…

记录到表

SET global general_log = 1;
SET global log_output = 'table';

然后你可以从我的mysql中选择。General_log表来检索最近的查询。

然后我可以在mysql.log上做一些类似于tail -f的事情,但是有更多的改进…

select * from mysql.general_log 
where  event_time  > (now() - INTERVAL 8 SECOND) and thread_id not in(9 , 628)
and argument <> "SELECT 1" and argument <> "" 
and argument <> "SET NAMES 'UTF8'"  and argument <> "SHOW STATUS"  
and command_type = "Query"  and argument <> "SET PROFILING=1"

这使得它很容易看到我的查询,我可以尝试和削减。我使用8秒的间隔来只获取最近8秒内执行的查询。

首先,请记住,这个日志文件在繁忙的服务器上可能会变得非常大。

对于mysql < 5.1.29:

要启用查询日志,请将其放在/etc/my.cnf的[mysqld]部分

log   = /path/to/query.log  #works for mysql < 5.1.29

另外,从MySQL控制台启用它

SET general_log = 1;

参见http://dev.mysql.com/doc/refman/5.1/en/query-log.html

mysql 5.1.29+

在mysql 5.1.29+中,日志选项已弃用。要指定日志文件并启用日志记录,请在my.cnf的[mysqld]部分中使用这个:

general_log_file = /path/to/query.log
general_log      = 1

另外,要从MySQL控制台打开日志记录(还必须以某种方式指定日志文件位置,或者找到默认位置):

SET global general_log = 1;

还要注意,还有其他选项可以只记录慢速查询或不使用索引的查询。

对于mysql>=5.5仅用于慢查询(1秒或更长时间) my.cfg

[mysqld]
slow-query-log = 1
slow-query-log-file = /var/log/mysql/mysql-slow.log
long_query_time = 1
log-queries-not-using-indexes