How to log all MySQL queries in Drupal

In order to benchmark a Drupal site performance you need to see all the database queries related to your Drupal site.

In case you don’t have access to the my.cnf file

If you don’t have access to the my.cnf file, you can log the queries from the Drupal code itself:

cp includes/database.mysql-common.inc includes/database.mysql-common.inc.backup.orig
vim includes/database.mysql-common.inc

At function db_query, just before the return clause, write:

error_log("Query: $query");

This will write all the queries Drupal uses for calculating its pages.
Be careful and use it with wisdom, as it might blow your error_log file.
To stop the queries logging process, overwrite the includes/database.mysql-common.inc file with the original.

cp includes/database.mysql-common.inc.backup.orig  includes/database.mysql-common.inc

In case you have access to the my.cnf file

If you have access to the my.cnf file, you need to set the log parameter (at the my.cnf file):

log = /var/log/mysql/mysql-queries.log

The output will be at

/var/log/mysql/mysql-queries.log

Please remember to shut the log off after you have finished, or it could hurt your site performance.
See MySQL queries log analyzing

MySQL Quiz
One Comment
  1. 5np says:

    how about drupal 7 ? Where paste this code ?

Leave a Reply

*