Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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