Writing database commands output to a file using Linux `tee` Command.

# mysql -u root -h 127.0.0.1
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.8 MemSQL source distribution (compatible; MySQL Enterprise & MySQL Commercial)
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql>
mysql>
mysql>
mysql>
mysql> ## Enbling the Logging to output file
mysql> tee /tmp/output.txt
Logging to file ‘/tmp/output.txt’
mysql> ## Executing the required commands like slow queries, stats, sql variable values – which will be having huge outputs.
mysql> select now();
+———————+
| now() |
+———————+
| 2014-06-24 02:05:19 |
+———————+
1 row in set (0.01 sec)

mysql> select * from information_schema.processlist where info is not null order by time desc ;
+—-+——+—————–+——+———+——+———–+————————————————————————————————+———+
| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO | PLAN_ID |
+—-+——+—————–+——+———+——+———–+————————————————————————————————+———+
| 2 | root | localhost:36140 | NULL | Query | 35 | executing | select * from information_schema.processlist where info is not null order by time desc limit 3 | NULL |
+—-+——+—————–+——+———+——+———–+————————————————————————————————+———+
1 row in set (0.01 sec)

mysql> select 3*3/2;
+——–+
| 3*3/2 |
+——–+
| 4.5000 |
+——–+
1 row in set (0.00 sec)
mysql> ## Disabling the Logging to output file
mysql> notee
Outfile disabled.
mysql>

Check whether the data has been appended to output file.

mysql> \! cat /tmp/output.txt
mysql> select now();
+———————+
| now() |
+———————+
| 2014-06-24 02:05:19 |
+———————+
1 row in set (0.01 sec)

mysql> select * from information_schema.processlist where info is not null order by time desc limit 3;
+—-+——+—————–+——+———+——+———–+————————————————————————————————+———+
| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO | PLAN_ID |
+—-+——+—————–+——+———+——+———–+————————————————————————————————+———+
| 2 | root | localhost:36140 | NULL | Query | 35 | executing | select * from information_schema.processlist where info is not null order by time desc limit 3 | NULL |
+—-+——+—————–+——+———+——+———–+————————————————————————————————+———+
1 row in set (0.01 sec)

mysql> select 3*3/2;
+——–+
| 3*3/2 |
+——–+
| 4.5000 |
+——–+
1 row in set (0.00 sec)

However, normal usage of Linux tee command can be see at below link.
http://linoxide.com/how-tos/tee-command-linux/

  • Ask Question