MySQL - How to track every query? [duplicate]

你。 提交于 2019-12-12 05:48:53

问题


Possible Duplicate:
How to enable MySQL Query Log?

Is there a way to track every SQL query in MySQL?

Im using Doctrine ORM to build entities and fetch them from database, and would like to see what queries is Doctrine producing. So when I refresh a webpage that uses Doctrine ORM to fetch some data from database, where could I see queries executed in database?

Is this possible?


回答1:


Enable MySQL's general query log:

The general query log is a general record of what mysqld is doing. The server writes information to this log when clients connect or disconnect, and it logs each SQL statement received from clients. The general query log can be very useful when you suspect an error in a client and want to know exactly what the client sent to mysqld.

Inside MySQL

set global general_log = 1;
set global log_output = 'table' /* so we can query it*/

select * from general_log       /* to select */

truncate general_log            /*if you got to much data*/

set global general_log = 0      /*to turn it off again*/



回答2:


Let this application decide if you want queries logged and log queries on MySQLs side if you want all queries logged.

Doctrine has a builtin logger interface and a demo logger in the package. From http://docs.doctrine-project.org/en/2.0.x/reference/configuration.html#sql-logger-optional :

<?php
$config->setSQLLogger($logger);
$config->getSQLLogger();

Gets or sets the logger to use for logging all SQL statements executed by Doctrine. The logger class must implement the Doctrine\DBAL\Logging\SQLLogger interface. A simple default implementation that logs to the standard output using echo and var_dump can be found at Doctrine\DBAL\Logging\EchoSQLLogger.`



来源:https://stackoverflow.com/questions/12880976/mysql-how-to-track-every-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!