MySQL Logging Using Tee

Posted: August 7, 2013 in Uncategorized

By using the –tee option when you invoke mysql, you can log statements and their output. All the data displayed on the screen is appended into a given file. This can be very useful for debugging purposes also. mysql flushes results to the file after each statement, just before it prints its
next prompt. Tee functionality works only in interactive mode.

You can enable this feature interactively with the tee command. Without a parameter, the previous file is used. The tee file can be disabled with the notee command. Executing tee again re-enables logging.

This feature can be enable using two way.

1. Before connecting to your MySQL database :-

[root@afzalkhan ~]# mysql –tee=/tmp/mysql.log
Logging to file ‘/tmp/mysql.log’
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 75
Server version: 5.0.95-log Source distribution

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> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| linux |
| mysql |
| test |
+——————–+
4 rows in set (0.00 sec)

mysql> exit
Bye

Now all above output can be found at /tmp/mysql.log file.

[root@afzalkhan ~]# cat /tmp/mysql.log
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 75
Server version: 5.0.95-log Source distribution

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> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| linux |
| mysql |
| test |
+——————–+
4 rows in set (0.00 sec)

mysql> exit
[root@afzalkhan~]#  

2. After connecting to your MySQL database :-

[root@afzalkhan ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 92
Server version: 5.1.66-log Source distribution

Copyright (c) 2000, 2012, 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> tee /tmp/mysql.log
Logging to file ‘/tmp/mysql.log’
mysql> select user from mysql.user limit 1;
+——-+
| user |
+——-+
| slave |
+——-+
1 row in set (0.00 sec)

mysql> exit
Bye

Now after enabling logging remaining part can be found at /tmp/mysql.log file.

[root@afzalkhan ~]# cat /tmp/mysql.log
mysql> select user from mysql.user limit 1;
+——-+
| user |
+——-+
| slave |
+——-+
1 row in set (0.00 sec)

mysql> exit
[root@afzalkhan ~]# 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s