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


当前回答

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

希望它能帮助到一些人。

其他回答

// To see global variable is enabled or not and location of query log    
SHOW VARIABLES like 'general%';
// Set query log on 
SET GLOBAL general_log = ON; 

这已经在一个评论中,但值得自己的回答: 无需编辑配置文件:在mysql中,以root身份执行

SET global general_log_file='/tmp/mysql.log'; 
SET global log_output = 'file';
SET global general_log = on;

之后别忘了关掉:

SET global general_log = off;

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

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.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

在Windows上,你可以简单地转到

C:\wamp\bin\mysql\mysql5.1.53\my.ini

在my.ini中插入这一行

general_log_file = c:/wamp/logs/mysql_query_log.log

my.ini文件最终看起来像这样

...
...
...    
socket      = /tmp/mysql.sock
skip-locking
key_buffer = 16M
max_allowed_packet = 1M
table_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
basedir=c:/wamp/bin/mysql/mysql5.1.53
log = c:/wamp/logs/mysql_query_log.log        #dump query logs in this file
log-error=c:/wamp/logs/mysql.log
datadir=c:/wamp/bin/mysql/mysql5.1.53/data
...
...
...
...