Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
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

2 Answers 2

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"
        HISTFILE="$OHISTFILE"
        HISTFILESIZE=$HISTFILESIZE
        mysql -c "INSERT ... $(<$HISTFILE).."
        rm $HISTFILE
      fi
    
share|improve this answer
    
This is linked to How to find out which ssh key was used. –  F. Hauri May 9 at 12:00

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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