I want to be able to setup a MySQL database and record all commands issued on my server by username and command. Is this possible, and if so, how would I set it up?
PROMPT_COMMANDif you want this in real-time (each line), this could be something like:
PROMPT_COMMAND='echo mysql -c "INSERT $(history 1|sed "s/^ *[0-9]* *//")"'
this could be stored in your
$HOME/.bash_logoutif you prefer digest at each
logout. this could let you begin your own script, try adding something like this to your
if [ "$HISTFILE" ] ;then export OHISTFILE="$HISTFILE" export HISTFILE=$(mktemp $HOME/.bash_history_XXXXXXXX) touch $HISTFILE history -a cat >>"$OHISTFILE" "$HISTFILE" HISTFILE="$OHISTFILE" HISTFILESIZE=$HISTFILESIZE mysql -c "INSERT ... $(<$HISTFILE).." rm $HISTFILE fi
If you are using bash or zsh, there is a project trying to accomplish this: advanced-shell-history .. Its abhorrent build system (read the Makefile before you go all
make install) might be the reason why it hasn't been included in distributions - am working on it and will push upstream, then report back :)
If you want a history for the plain mysql cli shell, rlwrap could be of use:
This package provides a small utility that uses the GNU readline library to allow the editing of keyboard input for any other command. Input history is remembered across invocations, separately for each command; history completion and search work as in bash and completion word lists can be specified on the command line.