Skip to content

Instantly share code, notes, and snippets.

@craig-davis
Last active September 29, 2017 21:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craig-davis/48be6978d55122677dec to your computer and use it in GitHub Desktop.
Save craig-davis/48be6978d55122677dec to your computer and use it in GitHub Desktop.
Enable and tail the MySQL General Log
regexp=\'.+?\'
colours=bold yellow
count=more
=====
regexp=(FROM|INNER JOIN|WHERE|AND|LIMIT|ON|AS|WHERE)
colours=bold green
count=more
======
regexp=(START TRANSACTION|COMMIT)
colours=bold white on_green
count=more
======
regexp=UPDATE
colours=bold yellow
count=more
======
regexp=SELECT
colours=bold blue
count=more
=====
regexp=DELETE
colours=bold red
count=more
#!/bin/bash
#
# mytail : Enable and tail the MySQL general log
#
# Craig Davis
# June 11, 2015
#
me=$(basename "$0")
requirements="tail"
colorizer="~/.grc.sql.conf"
# _getarg - simple variant of getopt
# example: when the following command "./foo --foo=bla -f bla --enabled -b" is executed with these calls:
# _getarg "foo"
# _getarg "f"
# _getarg "enabled"
# b=$(_getarg "b"); echo "$b"
# output: bla
# bla
# 1
# 1
args=$(echo "$@" | sed "s/ \-\- .*//g") # strip after '--' (usually means end of options)
_getarg(){
local arg_exist=$(echo "$args" | grep -e "-$1" && echo 1)
local lastarg=""
if [ "$arg_exist" ]; then
for arg in $args; do
local match=$( echo "$arg" | grep "\-$1" && echo 1 )
if [ "$match" ]; then
local arg_has_value=$(echo "$arg" | grep -e "-$1[=| ][A-Za-z0-9]" && echo 1)
if [ ! "$arg_has_value" ] ; then echo "1"; return 0; else
local assign=$(echo "$arg" | sed "s/.*=//g" 2>/dev/null )
if [ ${#assign} == 0 ]; then echo "$lastarg"; return 0;
else echo "$assign"; return 0; fi
fi
lastarg="$arg"
fi
done
fi
return 1;
}
mysql_log(){
dbuser=$(_getarg "user");
dbpass=$(_getarg "password");
if [ -z $dbuser ]; then
echo "Enable MySQL general log and tail output."
echo ""
echo "usage:"
echo " mytail --user=root --password=somepassword"
echo ""
echo "If password is not given it's asked from the tty."
echo ""
echo "ctrl+c to exit and disable logging"
exit 1;
fi
if [ -z $dbpass ]; then
read -s -p "Enter password: " dbpass
fi
printf "Turning on MySQL logging..."
mysql -u$dbuser -p$dbpass -e"SET GLOBAL general_log = 'ON';"
if [ $? -ne 0 ]; then
exit 1;
fi
printf "done.\n"
loginfo=$(mysql -u$dbuser -p$dbpass -e"show variables like 'general\_log\_file';");
loginfo=${loginfo:37}
printf "Tail general_log at $loginfo...\n\n"
trap _mysql_log_shutdown SIGINT
if [ ! -f $colorizer ]; then
sudo tail -f $loginfo
else
sudo grc -c $colorizer tail -f $loginfo
fi
}
_mysql_log_shutdown()
{
printf "\n\nTurning off MySQL logging..."
mysql -u$dbuser -p$dbpass -e"SET GLOBAL general_log = 'OFF';"
printf "done.\nGoodbye.\n"
return $?
}
checkrequirements(){
for req in $requirements; do
hash "$req" 2>&-
if [ $? == 1 ]; then echo "You need to install '$req'"; exit; fi
done;
}
checkrequirements
mysql_log
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment