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?
2 Answers
You could
play with
PROMPT_COMMAND
if 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/.bashrc
.play with
$HOME/.bash_logout
if you prefer digest at eachlogout
. this could let you begin your own script, try adding something like this to your.profile
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.
.profile
file and restrict the editing of this file.