Sometimes we use the mysql client to log in to the command line to execute some sql, and we want to record these contents during the operation. How to do it? For example, when we use source a sql file, I want to see if there is a problem in the source process. If the sql file is huge, the error log disappears from the screen very quickly before it can be viewed. How does it work?
At this time, we can use the tee command in the mysql command line to open a log file, and all subsequent operations of the same session will be recorded in this file.
Such as:
mysql> tee hello.log
mysql>select now() ;
mysql>exit; //Exit mysql client
View the contents of the hello.log file as follows:
+---------------------+
| now() |
+---------------------+
| 2018-03-07 10:55:58 |
+---------------------+
1 row in set (0.00 sec)
mysql> exit
You can specify a directory for tee's log files, such as:
mysql> tee ~/hello.log
Reference link: https://techglimpse.com/redirect-output-of-mysql-source-command-to-log-file/
So if you want to redirect output of MySQL source
command into a log file, then you need to use ‘tee
‘. Here’s how it works.
Step 1: Login to MySQL
Step 2: In the MySQL command prompt, type the below command.
mysql> tee log.out Logging to file 'log.out' mysql>
You should see a message “Logging to file” displayed on the screen.
Step 3: Now, source the SQL file
mysql> source sample.sql
Step 4: The output of source
command would have been logged into a file log.out
.
Step 5: View the log.out
$ more log.out
That’s it!
Why would you (or Mr. Waseem) wants to log the output of source SQL? For example, the SQL file that you would like to import might come with long list of tables & records and while sourcing it, there are chances few of the records might not get imported. Now what if you want to know which of those records failed during import? The log file will help you to find that.
Note: 'tee'
command can be used before executing any MySQL statement. The output of any query after logging the session will be stored in the specified file.
For example:
mysql> tee showdboutput.out Logging to file 'showdboutput.out' mysql> show databases;
The output of the above query will be stored in showdboutput.out
file.