Walker News

MySQL Dummy Guide: How To Output SQL Results To A Log File?

The third MySQL dummy guide in WalkerNews.net, is about how to get MySQL Command-line Tool (i.e. mysql) to simultaneously output SQL results onto the screen and write a copy to log file?

Again, there is no complicated setup required for this job and it’s tested on MySQL server version 4.1.7 running on my RHEL4 system.

The tee mysql command gets the SQL result output to two places at the same time, just like the tee Linux command does.

To help yourself visualize the underlying process, you may consider “tee” as a Y-piping that channel one input to two outputs at one go. In this case, the input is SQL result and the outputs are standard output (i.e. the server console or display) and log file.

MySQL Command-line Tool: How to direct SQL results to standard output and log file concurrently?

Similar to pager mysql command, you can run tee sqlout.log before executing a SQL statement (please replace sqlout.log to a log file name that’s meaningful to you):
mysql> tee sqlout.log
Logging to file 'sqlout.log'
mysql> select post_author, post_content, post_title from wp_posts;

You may also specify --tee option switch when invoking mysql so that every SQL results will be Y-piped:
[walker@walkernews.net ~]# mysql --tee sqlout.log -u wakernewsadmin -p
Logging to file 'sqlout.log'
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7 to server version: 4.1.7

To disable tee-piping, i.e. not to output SQL results to log file, simply execute notee:
mysql> notee
Outfile disabled.

Custom Search

  1. Rick Tack 12-06-09@17:05

    Hi, it seems everywhere I look for information about the tee command, nobody says anything about where (which directory) the specified log file will be written. I have looked in the directory where the mysql is being run and the directory where the mysql programme is running but I cannot find the log file I specified.

  2. sandeep singh 20-04-10@14:42

    i want to see the output of sql from basic

  3. V3ngencE 24-01-11@16:52

    its loged in the directory that you initiated the mysql command form.

    ie if you’re in root/tmp it will be saved in there

2018  •  Privacy Policy