MySQL 使用tee记录语句和输出日志
发表于:2025-01-31 作者:千家信息网编辑
千家信息网最后更新 2025年01月31日,在mysql命令行中,使用tee命令,可以记录语句和输出到指定文件。在debugging时会很有用。每执行一条语句,mysql都会讲执行结果刷新到指定文件。Tee功能只在交互模式生效。mysql> t
千家信息网最后更新 2025年01月31日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)
日志
命令
语句
文件
生成
输出
功能
参数
有用
模式
结果
登录
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
2伴奏软件开发
关系数据库理论及应用
发展网络技术的对策
人防软件数据库数据恢复
摩纹网络技术有限公司
实用网络技术基础试卷
网络安全画画简单又漂亮手抄报
桌面上的服务器角色配置
江阴中科曙光服务器维修价格
数据库显示1001
数据库中设置主键升序
如何成为一个网络安全员
冠成互联网科技
数据库的列无法更改
虚拟网络技术研究报告
吉林忠友网络技术
mysql数据库账号导出
保证网站服务器的安全性
老头环进不去服务器
软件开发专业论文题目写什么好
查询数据库改变下拉框的值
吉安软件开发
联想服务器更加装硬盘后配置
青少年如何网络安全
网络安全两会期间
大展网络技术
请简述什么是云服务器
天津ipfs服务器配置云空间
越南网络安全法表决通过
计算机网络安全结构体系图