November 27, 2014

Logging your MySQL command line client sessions

Baron recently wrote about very helpful but often forgotten about “Pager” feature of command line client. There is another one which falls into the same list – the –tee option.

Specifying –tee=/path/to/file.log you can get all session content (everything typed in and printed out) stored in the log file. Quite handy for example to keep track of changes done on production.

Moreover you can put tee=/logs/mysql.log in “mysql” section in my.cnf to have logging enabled automatically when you start the client.

If you’re looking to log session beyond MySQL command line client you can check out “script” tool.

About Peter Zaitsev

Peter managed the High Performance Group within MySQL until 2006, when he founded Percona. Peter has a Master's Degree in Computer Science and is an expert in database kernels, computer hardware, and application scaling.


  1. Tom Krouper says:

    I’ve heard of (and overlooked) the tee mysql client option. I’ve never heard of the “script” tool. Could you please let me know where I would find the “script” tool?


    PS: I think I know a lot about MySQL, then I find another thing I’d never heard of. :-)

  2. peter says:

    Well “man script” does it on my Linux distribution if you do not have it installed you can check some online man pages to see if it will work for you

  3. Tom Krouper says:

    I was thinking it was another MySQL tool. Perfect, thanks.

  4. Another way to do this is to use screen. CTRL-A H will log the screen to a file.

  5. peter says:

    Right. SCREEN is the tool for many needs :)

  6. James Cohen says:

    You can also access this from the MySQL client prompt:

    mysql> tee /tmp/session_log_file.txt
    Logging to file ‘/tmp/session_log_file.txt
    mysql> notee
    Outfile disabled.

  7. I almost always use screen when diagnosing server issues or running OS updates.

    I can have a mytop, top and other tools running and toggle between them easily. As mentioned, screen has a logging option tool.

    I wrote a screen tutorial some time ago:

  8. peter says:

    Thanks Jeff

  9. No problem. I think screen is an oft-overlooked tool. Not related to logging, but for one client we have an admin box with mytop installed. They have 5 database servers in a master/slave farm setup. I have aliases setup to launch mytop and have it connect to each system. (e.g. mytopdb1 connects mytop to db1). I do this from inside screen and then can quickly toggle through mytop output on all boxes.

    There are probably cleaner ways to do this but I’ve found it handy to quickly check how queries are spreading over the slave pool as the load balancing is controlled on the application side.

  10. James Parks says:


    Learn something new every day!

Speak Your Mind