如何查看实时 MySQL 查询?

2021-11-20 00:00:00 mysql monitoring

如何在 Linux 服务器上跟踪发生的 MySQL 查询?

How can I trace MySQL queries on my Linux server as they happen?

例如,我想设置某种侦听器,然后请求一个网页并查看引擎执行的所有查询,或者只是查看在生产服务器上运行的所有查询.我该怎么做?

For example I'd love to set up some sort of listener, then request a web page and view all of the queries the engine executed, or just view all of the queries being run on a production server. How can I do this?

推荐答案

您可以运行 MySQL 命令 SHOW FULL PROCESSLIST; 以查看在任何给定时间正在处理哪些查询,但这可能会赢没有达到你所希望的.

You can run the MySQL command SHOW FULL PROCESSLIST; to see what queries are being processed at any given time, but that probably won't achieve what you're hoping for.

无需修改使用服务器的每个应用程序即可获取历史记录的最佳方法可能是通过触发器.您可以设置触发器,以便每次查询运行都会将查询插入到某种历史记录表中,然后创建一个单独的页面来访问这些信息.

The best method to get a history without having to modify every application using the server is probably through triggers. You could set up triggers so that every query run results in the query being inserted into some sort of history table, and then create a separate page to access this information.

请注意,这可能会大大减慢服务器上的所有内容,并在每个查询之上添加额外的 INSERT.

Do be aware that this will probably considerably slow down everything on the server though, with adding an extra INSERT on top of every single query.

另一种选择是一般查询日志,但是将其写入平面文件会消除显示灵活性的很多可能性,尤其是实时显示.如果您只是想要一种简单、易于实施的方式来查看发生了什么,启用 GQL 然后在日志文件上运行 tail -f 就可以了.

another alternative is the General Query Log, but having it written to a flat file would remove a lot of possibilities for flexibility of displaying, especially in real-time. If you just want a simple, easy-to-implement way to see what's going on though, enabling the GQL and then using running tail -f on the logfile would do the trick.

相关文章