Skip to content

Instantly share code, notes, and snippets.

@juampynr
Last active April 25, 2017 17:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save juampynr/9627425 to your computer and use it in GitHub Desktop.
Save juampynr/9627425 to your computer and use it in GitHub Desktop.
Mysql log all queries into a file

Installation

Add the following to your .bashrc or .profile file in your user's home path:

# Log all queries.
function mysqlLog {
  drush sql-cli << EOF
SET GLOBAL query_cache_type=OFF;
SET global log_output = 'FILE';
SET global general_log_file='/tmp/query.log';
SET global general_log = 1;
EOF
  echo "Queries will be saved at /tmp/query.log"
}

# Stop logging all queries.
function mysqlUnlog {
  sudo service mysql restart
}

Usage

Start logging queries

$ mysqlLog
Queries will be saved at /tmp/query.log

Then, open a local website or run a command that executes MySQL queries. When you are done run the following to stop logging:

$ mysqlUnlog
[sudo] password for juampy: 
mysql stop/waiting
mysql start/running, process 24464

Logged queries can be found at /tmp/query.log.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment