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?

  • do you want this in real time or periodically updated - for example once a day? I would suggest regular review of users history file but it will left you with possibility of users removing specific entries from their history files. But there is a tool called script and it can log all commands to a txt file. You can add script command to users .profile file and restrict the editing of this file. – mnmnc Dec 17 '12 at 14:17

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 each logout. 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"
        mysql -c "INSERT ... $(<$HISTFILE).."
        rm $HISTFILE

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.