Suddenly I Tee .. or how to log your mysql shell output to a log file
A lot of you may already know this, but I am willing to bet there are more that don’t. I’m talking about the tee command in the bash shell, and in MySQL. For our purposes, we’ll talk about the tee command in MySQL.
Problem: You have a series of SQL statements whose results take up a few screens worth of output, and you need to take this output and send it to someone else (A DBA, MySQL Support, your mentor). You could just do a copy/paste from your terminal, but what if you realized in the end that your scroll back buffer isn’t as large as you thought it was?
Solution: Tee. Apparently, the mysql client comes with tee.
mysqlshell> tee mysqlog.sql ;
Logging to file ‘mysqlog.sql’
use dbname;
select foo from bar;
….
mysqlshell> notee;
quit;
bash> cat mysqlog.sql
This could be filed under “my bag of neat mysql tricks”
PS: Sorry for the K.T Tunstall song reference/joke.. I heard it on Saturday and it’s not leaving my head.
Good Tip, some other ways to do this:
From Bash
$> mysql dbname -e ’select foo from bar where name=”Joe” ‘ > output.sql
From mysql
mysql> SELECT foo FROM bar INTO OUTFILE ‘output.sql’
(this file will be inside the /var/lib/mysql/foo/ dir unless you give full path)
Also check out mysqldump if you want to copy an entire database or table from one server to another (or for a backup).