Skip to content

Instantly share code, notes, and snippets.

@claytonaalves
Created July 3, 2012 23:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claytonaalves/3044166 to your computer and use it in GitHub Desktop.
Save claytonaalves/3044166 to your computer and use it in GitHub Desktop.
Real-time MySQL query log analyzer with source highlight
#!/bin/bash
# Author : Clayton A. Alves
# License : I don't care...
# Date : 2012-06-06
#
# Real-time MySQL query log analyzer with source highlight
# (displays executed queries colorized on screen)
#
# Requires:
# - Gnu Source-highlight (http://www.gnu.org/software/src-highlite/source-highlight.html)
#
clear
cleanup()
{
mysql -e "SET GLOBAL general_log='OFF'"
echo -e '\n---> MySQL log disabled!'
exit 0
}
trap cleanup SIGINT
> /var/run/mysqld/mysqld.log
chown mysql:mysql /var/run/mysqld/mysqld.log
mysql mysql <<EOF
SET GLOBAL log_output='FILE';
SET GLOBAL general_log='ON';
EOF
echo '--- Executed queries ---------------------'
tail -n +4 -f /var/run/mysqld/mysqld.log | sed -u -e 's/^.\+Query\t//' -e '/^.\+Connect\t/d' -e '/^.\+Quit\t/d' | source-highlight -fesc -ssql -oSTDOUT
echo '--------------------------------------------'
echo
mysql mysql <<EOF
SET GLOBAL general_log='OFF';
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment