MySQL 使用tee記錄語句和輸出日誌
在mysql命令列中,使用tee命令,可以記錄語句和輸出到指定檔案。在debugging時會很有用。
每執行一條語句,mysql都會講執行結果重新整理到指定檔案。Tee功能只在互動模式生效。
mysql> tee /tmp/20160908.log
Logging to file '/tmp/20160908.log'
mysql> show databases;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 4
Current database: test
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 sec)
mysql> use test
Database changed
mysql> show tables;
+--------------------+
| Tables_in_test |
+--------------------+
| ADDSubscribers |
| Subscribers |
| dept |
| emp |
| t10 |
| t2 |
| t5 |
| t50 |
| t7 |
| test |
| tmp_Subscribers_01 |
| tmp_Subscribers_02 |
| tmp_Subscribers_03 |
| version |
+--------------------+
14 rows in set (0.00 sec)
mysql> select * from emp;
+-------+--------+-----------+------+---------------------+------+------+--------+
| empno | ename | job | mgr | hiredate | sal | com | deptno |
+-------+--------+-----------+------+---------------------+------+------+--------+
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800 | NULL | 20 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600 | 300 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250 | 500 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975 | NULL | 20 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250 | 1400 | 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-07-13 00:00:00 | 3000 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500 | 0 | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1987-07-13 00:00:00 | 1100 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000 | NULL | 20 |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300 | NULL | 10 |
+-------+--------+-----------+------+---------------------+------+------+--------+
14 rows in set (0.03 sec)
使用notee命令來關閉日誌記錄
mysql> notee
Outfile disabled.
檢視生成的日誌
[root@localhost fire]# cat /tmp/20160908.log
mysql> show databases;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 4
Current database: test
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 sec)
mysql> use test
Database changed
mysql> show tables;
+--------------------+
| Tables_in_test |
+--------------------+
| ADDSubscribers |
| Subscribers |
| dept |
| emp |
| t10 |
| t2 |
| t5 |
| t50 |
| t7 |
| test |
| tmp_Subscribers_01 |
| tmp_Subscribers_02 |
| tmp_Subscribers_03 |
| version |
+--------------------+
14 rows in set (0.00 sec)
mysql> select * from emp;
+-------+--------+-----------+------+---------------------+------+------+--------+
| empno | ename | job | mgr | hiredate | sal | com | deptno |
+-------+--------+-----------+------+---------------------+------+------+--------+
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800 | NULL | 20 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600 | 300 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250 | 500 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975 | NULL | 20 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250 | 1400 | 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-07-13 00:00:00 | 3000 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500 | 0 | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1987-07-13 00:00:00 | 1100 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000 | NULL | 20 |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300 | NULL | 10 |
+-------+--------+-----------+------+---------------------+------+------+--------+
14 rows in set (0.03 sec)
mysql> notee
當呼叫mysql時,使用--tee引數,可以記錄這個登入會話的全部日誌。
[root@localhost ~]# mysql -uroot -p --tee=/tmp/20160908_02.log
Logging to file '/tmp/20160908_02.log'
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.31-77.0-log Percona Server (GPL), Release 77.0, Revision 5c1061c
Copyright (c) 2009-2016 Percona LLC and/or its affiliates
Copyright (c) 2000, 2016, 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> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from test;
+------+-------+
| id | name |
+------+-------+
| 10 | neo |
| 20 | John |
| 30 | Lucy |
| 40 | Larry |
| 50 | Lilly |
+------+-------+
5 rows in set (0.04 sec)
檢視生成的日誌
[root@localhost fire]# cat /tmp/20160908_02.log
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.31-77.0-log Percona Server (GPL), Release 77.0, Revision 5c1061c
Copyright (c) 2009-2016 Percona LLC and/or its affiliates
Copyright (c) 2000, 2016, 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> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from test;
+------+-------+
| id | name |
+------+-------+
| 10 | neo |
| 20 | John |
| 30 | Lucy |
| 40 | Larry |
| 50 | Lilly |
+------+-------+
5 rows in set (0.04 sec)
每執行一條語句,mysql都會講執行結果重新整理到指定檔案。Tee功能只在互動模式生效。
mysql> tee /tmp/20160908.log
Logging to file '/tmp/20160908.log'
mysql> show databases;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 4
Current database: test
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 sec)
mysql> use test
Database changed
mysql> show tables;
+--------------------+
| Tables_in_test |
+--------------------+
| ADDSubscribers |
| Subscribers |
| dept |
| emp |
| t10 |
| t2 |
| t5 |
| t50 |
| t7 |
| test |
| tmp_Subscribers_01 |
| tmp_Subscribers_02 |
| tmp_Subscribers_03 |
| version |
+--------------------+
14 rows in set (0.00 sec)
mysql> select * from emp;
+-------+--------+-----------+------+---------------------+------+------+--------+
| empno | ename | job | mgr | hiredate | sal | com | deptno |
+-------+--------+-----------+------+---------------------+------+------+--------+
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800 | NULL | 20 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600 | 300 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250 | 500 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975 | NULL | 20 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250 | 1400 | 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-07-13 00:00:00 | 3000 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500 | 0 | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1987-07-13 00:00:00 | 1100 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000 | NULL | 20 |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300 | NULL | 10 |
+-------+--------+-----------+------+---------------------+------+------+--------+
14 rows in set (0.03 sec)
使用notee命令來關閉日誌記錄
mysql> notee
Outfile disabled.
檢視生成的日誌
[root@localhost fire]# cat /tmp/20160908.log
mysql> show databases;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 4
Current database: test
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 sec)
mysql> use test
Database changed
mysql> show tables;
+--------------------+
| Tables_in_test |
+--------------------+
| ADDSubscribers |
| Subscribers |
| dept |
| emp |
| t10 |
| t2 |
| t5 |
| t50 |
| t7 |
| test |
| tmp_Subscribers_01 |
| tmp_Subscribers_02 |
| tmp_Subscribers_03 |
| version |
+--------------------+
14 rows in set (0.00 sec)
mysql> select * from emp;
+-------+--------+-----------+------+---------------------+------+------+--------+
| empno | ename | job | mgr | hiredate | sal | com | deptno |
+-------+--------+-----------+------+---------------------+------+------+--------+
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800 | NULL | 20 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600 | 300 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250 | 500 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975 | NULL | 20 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250 | 1400 | 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-07-13 00:00:00 | 3000 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500 | 0 | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1987-07-13 00:00:00 | 1100 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000 | NULL | 20 |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300 | NULL | 10 |
+-------+--------+-----------+------+---------------------+------+------+--------+
14 rows in set (0.03 sec)
mysql> notee
當呼叫mysql時,使用--tee引數,可以記錄這個登入會話的全部日誌。
[root@localhost ~]# mysql -uroot -p --tee=/tmp/20160908_02.log
Logging to file '/tmp/20160908_02.log'
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.31-77.0-log Percona Server (GPL), Release 77.0, Revision 5c1061c
Copyright (c) 2009-2016 Percona LLC and/or its affiliates
Copyright (c) 2000, 2016, 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> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from test;
+------+-------+
| id | name |
+------+-------+
| 10 | neo |
| 20 | John |
| 30 | Lucy |
| 40 | Larry |
| 50 | Lilly |
+------+-------+
5 rows in set (0.04 sec)
檢視生成的日誌
[root@localhost fire]# cat /tmp/20160908_02.log
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.31-77.0-log Percona Server (GPL), Release 77.0, Revision 5c1061c
Copyright (c) 2009-2016 Percona LLC and/or its affiliates
Copyright (c) 2000, 2016, 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> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from test;
+------+-------+
| id | name |
+------+-------+
| 10 | neo |
| 20 | John |
| 30 | Lucy |
| 40 | Larry |
| 50 | Lilly |
+------+-------+
5 rows in set (0.04 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2124680/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Laravel 中輸出 SQL 語句的到 log 日誌LaravelSQL
- 操作日誌記錄(包括輸出至自定義日誌檔案)
- mysql 開啟和關閉日誌記錄MySql
- mysql開啟日誌記錄MySql
- 如何在putty上完全記錄操作linux的日誌_script_tee_screenLinux
- 記錄騰訊雲使用日誌
- php日誌,記錄日誌PHP
- 使用Redis記錄系統日誌Redis
- 使用Rsyslog記錄Apache日誌Apache
- 使用 .NET Core 的日誌記錄
- SSH框架控制檯輸出HQL語句和SQL語句的方法框架SQL
- MySQL 記錄所有執行了的 sql 語句MySql
- mysql快速新增百萬條記錄的語句MySql
- [Mysql 查詢語句]——查詢指定記錄MySql
- reportbuilder 日誌輸出UI
- Python輸出日誌Python
- 日誌記錄器
- .NET Core使用Nlog記錄日誌
- jsqlparser使用記錄---生成sql語句JSSQL
- ffmpeg使用tee實現單次編碼多路輸出
- mysql誤修改全表記錄,使用binlog日誌找回資料MySql
- mysql audit-訪問日誌記錄應用MySql
- Hibernate 使用log4j輸出日誌
- 使用Log4j2輸出日誌演示
- python如何輸出日誌?Python
- Java日誌輸出問題Java
- awk多行日誌排序輸出排序
- MySQL日誌(一條sql更新語句是如何執行的)MySql
- 通過日誌檢視mysql正在執行的SQL語句MySql
- win8 學習筆記二 輸出日誌筆記
- Swoft AOP 記錄使用者操作日誌
- Laravel sql 日誌記錄LaravelSQL
- secureCRT記錄操作日誌Securecrt
- 記錄日誌檔案
- PHP日誌記錄方法PHP
- oracle日誌操作記錄Oracle
- SQL語句收縮日誌檔案SQL
- .net core 中使用Log4net輸出日誌到Mysql資料庫中MySql資料庫