简体   繁体   English

如何记录MySQL查询?

[英]How can I log MySQL queries?

Is it possible to log all queries that are executed? 是否可以记录所有已执行的查询? I am looking at a database that is accessed by many different apps. 我正在查看可被许多不同应用访问的数据库。 One of them is modifying a table's value in a way it should not. 其中之一是以不应该的方式修改表的值。 I am trying figure out which app is the culprit. 我正在尝试找出哪个应用程序是罪魁祸首。 It would help me out a lot if I can capture all the queries that are executed on that table and at what time. 如果我可以捕获在该表上以及在什么时候执行的所有查询,那将对我有很大帮助。

Many thanks in advance for your help. 在此先感谢您的帮助。

Either use the --log[=file_name] command line switch on mysqld or edit/create a my.cnf containing: 在mysqld上使用--log[=file_name]命令行开关,或编辑/创建包含以下内容的my.cnf

[mysqld]
log=/tmp/mysql.log

Explained fully in this article . 本文对此作了充分的解释。

As far as I am aware, there are currently no auditing capabilities built in to MySQL. 据我所知,MySQL目前没有内置的审计功能。 Log queries from within the applications that generate them, or sniff connections to the server. 从生成查询的应用程序中记录查询,或嗅探到服务器的连接。

in your .ini configuration add this line 在您的.ini配置中添加此行

log=allqueries.log

you will need to restart mysql 您将需要重新启动mysql

A possible solution to your problem is to utilize an update trigger on the table in question. 解决您的问题的一种可能的方法是利用有关表的更新触发器。 The trigger will be fired on any update to the table, and it possible to write the trigger such that when it meets certain criteria (the value in question is changed), an action is performed (perhaps writing to a temporary table, the SQL statement that makes the change). 对该表的任何更新都将触发该触发器,并且可以编写该触发器,以便使其在满足某些条件(所讨论的值已更改)时执行操作(例如写入临时表,SQL语句)做出改变)。 For more information, I suggest looking at Trigger Syntax . 有关更多信息,建议您查看触发器语法

You can use the general log in MySQL to achieve this . 您可以使用MySQL中的常规日志来实现此目的 I only recommend you do that on a test/development database without many concurrent users, because the amount of output generated is huge . 我只建议您在没有许多并发用户的情况下在测试/开发数据库上执行此操作,因为生成的输出量很大 I'm not sure if it logs the timestamp, though. 我不确定它是否记录时间戳。

If it doesn't, on a unix/linux setup, I'd say write a simple script that read lines from the stdin and print the lines with the current timestamp when they were read, and pipe tail -f on the log file to it, so you can add your own timestamps. 如果没有,我会说编写一个简单的脚本,该脚本从stdin读取行,并在读取行时以当前时间戳打印行,并将日志文件中的tail -f传递给它,因此您可以添加自己的时间戳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM