[英]How to enable MySQL Query Log?
How do I enable the MySQL function that logs each SQL query statement received from clients and the time that query statement has submitted?如何启用 MySQL function 记录从客户端收到的每个 SQL 查询语句以及查询语句提交的时间? Can I do that in phpmyadmin or NaviCat?
我可以在 phpmyadmin 或 NaviCat 中这样做吗? How do I analyse the log?
如何分析日志?
First, Remember that this logfile can grow very large on a busy server.首先,请记住,此日志文件在繁忙的服务器上可能会变得非常大。
For mysql < 5.1.29:对于 mysql < 5.1.29:
To enable the query log, put this in /etc/my.cnf
in the [mysqld]
section要启用查询日志,请将其放在
[mysqld]
部分的/etc/my.cnf
中
log = /path/to/query.log #works for mysql < 5.1.29
Also, to enable it from MySQL console另外,从 MySQL 控制台启用它
SET general_log = 1;
See http://dev.mysql.com/doc/refman/5.1/en/query-log.html见http://dev.mysql.com/doc/refman/5.1/en/query-log.html
For mysql 5.1.29+对于 mysql 5.1.29+
With mysql 5.1.29+ , the log
option is deprecated.在 mysql 5.1.29+ 中,不推荐使用
log
选项。 To specify the logfile and enable logging, use this in my.cnf in the [mysqld]
section:要指定日志文件并启用日志记录,请在
[mysqld]
部分的 my.cnf 中使用它:
general_log_file = /path/to/query.log
general_log = 1
Alternately, to turn on logging from MySQL console (must also specify log file location somehow, or find the default location):或者,要从 MySQL 控制台打开日志记录(还必须以某种方式指定日志文件位置,或找到默认位置):
SET global general_log = 1;
Also note that there are additional options to log only slow queries, or those which do not use indexes.另请注意,还有其他选项可以仅记录慢查询或不使用索引的查询。
Take a look on this answer to another related question.看看这个对另一个相关问题的回答。 It shows how to enable, disable and to see the logs on live servers without restarting.
它展示了如何在不重新启动的情况下启用、禁用和查看实时服务器上的日志。
Log all queries in mysql记录mysql中的所有查询
Here is a summary:这是一个总结:
If you don't want or cannot restart the MySQL server you can proceed like this on your running server:如果您不想或无法重新启动 MySQL 服务器,您可以在正在运行的服务器上进行如下操作:
Enable Query logging on the database (Note that the string 'table' should be put literally and not substituted by any table name. Thanks Nicholas Pickering )在数据库上启用查询日志记录(请注意,字符串 'table' 应该按字面意思放置,而不是用任何表名替换。谢谢Nicholas Pickering )
SET global general_log = 1;
SET global log_output = 'table';
select * from mysql.general_log;
SET global general_log = 0;
TRUNCATE mysql.general_log
This was already in a comment, but deserves its own answer: Without editing the config files: in mysql, as root, do这已经在评论中,但值得自己回答:无需编辑配置文件:在 mysql 中,以 root 身份执行
SET global general_log_file='/tmp/mysql.log';
SET global log_output = 'file';
SET global general_log = on;
Don't forget to turn it off afterwards:之后不要忘记关闭它:
SET global general_log = off;
I use this method for logging when I want to quickly optimize different page loads.当我想快速优化不同的页面加载时,我使用这种方法进行日志记录。 It's a little tip...
这是一个小技巧...
Logging to a TABLE记录到表
SET global general_log = 1;
SET global log_output = 'table';
You can then select from my mysql.general_log
table to retrieve recent queries.然后您可以从我的
mysql.general_log
表中选择以检索最近的查询。
I can then do something similar to tail -f
on the mysql.log, but with more refinements...然后我可以在 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"
This makes it easy to see my queries that I can try and cut back.这使我可以轻松查看我可以尝试并减少的查询。 I use 8 seconds interval to only fetch queries executed within the last 8 seconds.
我使用 8 秒间隔来仅获取在过去 8 秒内执行的查询。
您可以禁用或启用常规查询日志(记录所有查询)
SET GLOBAL general_log = 1 # (or 0 to disable)
I also wanted to enable the MySQL log file to see the queries and I have resolved this with the below instructions我还想启用 MySQL 日志文件以查看查询,并且我已按照以下说明解决了此问题
/etc/mysql/mysql.conf.d
/etc/mysql/mysql.conf.d
and enable the below lines并启用以下几行
general_log_file = /var/log/mysql/mysql.log
general_log = 1
/etc/init.d/mysql restart
/etc/init.d/mysql restart
MySQL /etc/init.d/mysql restart
/var/log/mysql/
and check the logs/var/log/mysql/
并检查日志// 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;
On Windows you can simply go to在 Windows 上,您只需转到
C:\wamp\bin\mysql\mysql5.1.53\my.ini
Insert this line in my.ini在my.ini 中插入这一行
general_log_file = c:/wamp/logs/mysql_query_log.log
The my.ini file finally looks like this 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
...
...
...
...
There is bug in MySQL 5.6 version. MySQL 5.6 版本中存在错误。 Even mysqld show as :
甚至 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
Realy settings are reading in following order :实际设置按以下顺序读取:
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
Check file: "C:\\ProgramData\\MySQL\\MySQL Server 5.6\\my.ini"检查文件:“C:\\ProgramData\\MySQL\\MySQL Server 5.6\\my.ini”
Hope it help somebody.希望它可以帮助某人。
for mysql>=5.5 only for slow queries (1 second and more) my.cfg对于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
To enable the query log in MAC Machine:在 MAC Machine 中启用查询日志:
Open the following file:打开以下文件:
vi /private/etc/my.cnf
Set the query log url under 'mysqld' section as follows:在 'mysqld' 部分下设置查询日志 url,如下所示:
[mysqld]
general_log_file=/Users/kumanan/Documents/mysql_query.log
Few machine's are not logging query properly, So that case you can enable it from MySQL console很少有机器没有正确记录查询,因此您可以从 MySQL 控制台启用它
mysql> SET global general_log = 1;
Not exactly an answer to the question because the question already has great answers.不完全是这个问题的答案,因为这个问题已经有了很好的答案。 This is a side info.
这是一个侧面信息。 Enabling general_log really put a dent on MySQL performance.
启用 general_log 确实会降低 MySQL 的性能。 I left
general_log =1
accidentally on a production server and spent hours finding out why performance was not comparable to a similar setup on other servers.我不小心在生产服务器上留下了
general_log =1
并花了几个小时找出为什么性能无法与其他服务器上的类似设置相提并论。 Then I found this which explains the impact of enabling general log.然后我发现this解释了启用通用日志的影响。 http://www.fromdual.com/general_query_log_vs_mysql_performance .
http://www.fromdual.com/general_query_log_vs_mysql_performance 。
Gist of the story, don't put general_log=1
in the .cnf
file.故事的要点,不要将
general_log=1
放在.cnf
文件中。 Instead use set global general_log =1
for a brief duration just to log enough to find out what you are trying to find out and then turn it off.相反,在短时间内使用
set global general_log =1
只是为了记录足够的信息以找出您想要找出的内容,然后将其关闭。
In phpMyAdmin 4.0, you go to Status > Monitor.在 phpMyAdmin 4.0 中,您转到状态 > 监控。 In there you can enable the slow query log and general log, see a live monitor, select a portion of the graph, see the related queries and analyse them.
在那里您可以启用慢查询日志和一般日志,查看实时监视器,选择图形的一部分,查看相关查询并分析它们。
I had to drop and recreate the general log at one point.我不得不一次性删除并重新创建一般日志。 During the recreation, character sets got messed up and I ended up having this error in the logs:
在娱乐过程中,字符集搞砸了,我最终在日志中出现了这个错误:
[ERROR] Incorrect definition of table mysql.general_log: expected the type of column 'user_host' at position 1 to have character set 'utf8' but found character set 'latin1'
So if the standard answer of "check to make sure logging is on" doesn't work for you, check to make sure your fields have the right character set.因此,如果“检查以确保登录”的标准答案对您不起作用,请检查以确保您的字段具有正确的字符集。
My OS Win10 , MySQL server version - 5.7我的操作系统Win10 , MySQL服务器版本 - 5.7
The path to my.ini my.ini 的路径
C:\ProgramData\MySQL\MySQL Server 5.7\my.ini
Just add into my.ini file只需添加到 my.ini 文件中
general_log_file = C:/ProgramData/MySQL/MySQL Server 5.7/mysql.log
general_log = 1
You may come across a set of Hexadecimal values, like this (argument column):您可能会遇到一组十六进制值,如下所示(参数列):
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)
so to make it readable, just use:所以为了使其可读,只需使用:
select a.*, convert(a.argument using utf8) from mysql.general_log a;
And the return is something like this:回报是这样的:
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: I used LIMIT 1
on examples, because my log table is too big. ps:我在例子上使用了
LIMIT 1
,因为我的日志表太大了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.