原生 Oracle 的 sqlplus 客户端工具提供了一个 spool 命令,可以用来将 SQL*Plus 会话中的所有屏幕输出保存到文件,特别适用于数据库管理员和开发人员生成报告、导出数据或记录操作日志。OceanBase 数据库的 OBClient 客户端工具提供了一个跟 MySQL 客户端工具的 tee 命令同名的命令,也可以实现同样的功能。
详细说明
可以使用 tee file_name 和 notee 命令来开始和停止捕获输出到文件
[root@observer1 ~]# obclient -h127.0.0.1 -usys@oracle -P2881 -pxxx
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3221501759
Server version: OceanBase 4.2.5.4 (r104000082025052817-2e9399047c72ee074510f39a687426e4c5649964) (Built May 28 2025 18:36:07)
Copyright (c) 2000, 2018, OceanBase and/or its affiliates. All rights reserved.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
obclient(SYS@oracle)[SYS]> tee /tmp/test.txt
Logging to file '/tmp/test.txt'
obclient(SYS@oracle)[SYS]> select now() from dual;
ORA-00600: internal error code, arguments: -5055, FUNCTION NOW does not exist
obclient(SYS@oracle)[SYS]> select systimestamp from dual;
+-------------------------------------+
| SYSTIMESTAMP |
+-------------------------------------+
| 05-JUN-25 11.19.15.003430 PM +08:00 |
+-------------------------------------+
1 row in set (0.001 sec)
obclient(SYS@oracle)[SYS]> notee
Outfile disabled.
obclient(SYS@oracle)[SYS]> Bye
对应捕获的 /tmp/test.txt 文件内容如下。
[root@observer1 ~]# cat /tmp/test.txt
obclient(SYS@oracle)[SYS]> select now() from dual;
ORA-00600: internal error code, arguments: -5055, FUNCTION NOW does not exist
obclient(SYS@oracle)[SYS]> select systimestamp from dual;
+-------------------------------------+
| SYSTIMESTAMP |
+-------------------------------------+
| 05-JUN-25 11.19.15.003430 PM +08:00 |
+-------------------------------------+
1 row in set (0.001 sec)
obclient(SYS@oracle)[SYS]> notee
tee 命令目前都是以追加的模式将内容写入到输出文件的
[root@observer1 ~]# obclient -h127.0.0.1 -usys@oracle -P2881 -pxxx
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3221512992
Server version: OceanBase 4.2.5.4 (r104000082025052817-2e9399047c72ee074510f39a687426e4c5649964) (Built May 28 2025 18:36:07)
Copyright (c) 2000, 2018, OceanBase and/or its affiliates. All rights reserved.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
obclient(SYS@oracle)[SYS]> tee /tmp/test.txt
Logging to file '/tmp/test.txt'
obclient(SYS@oracle)[SYS]>
obclient(SYS@oracle)[SYS]>
obclient(SYS@oracle)[SYS]> select systimestamp from dual;
+-------------------------------------+
| SYSTIMESTAMP |
+-------------------------------------+
| 05-JUN-25 11.20.16.074102 PM +08:00 |
+-------------------------------------+
1 row in set (0.000 sec)
obclient(SYS@oracle)[SYS]> Bye
对应捕获的 /tmp/test.txt 文件内容如下。
[root@observer1 ~]# cat /tmp/test.txt
obclient(SYS@oracle)[SYS]> select now() from dual;
ORA-00600: internal error code, arguments: -5055, FUNCTION NOW does not exist
obclient(SYS@oracle)[SYS]> select systimestamp from dual;
+-------------------------------------+
| SYSTIMESTAMP |
+-------------------------------------+
| 05-JUN-25 11.19.15.003430 PM +08:00 |
+-------------------------------------+
1 row in set (0.001 sec)
obclient(SYS@oracle)[SYS]> notee
obclient(SYS@oracle)[SYS]>
obclient(SYS@oracle)[SYS]>
obclient(SYS@oracle)[SYS]> select systimestamp from dual;
+-------------------------------------+
| SYSTIMESTAMP |
+-------------------------------------+
| 05-JUN-25 11.20.16.074102 PM +08:00 |
+-------------------------------------+
1 row in set (0.000 sec)
说明:如果想以覆盖的模式将屏幕输出写入到文件,可以先手工清空该文件。 示例如下。
[root@observer1 ~]# :> /tmp/test.txt
或者:
obclient(SYS@oracle)[SYS]> system :> /tmp/test.txt
也可以将 --tee=file_name 作为 OBClient 命令的附加选项直接使用
[root@observer1 ~]# obclient -h127.0.0.1 -usys@oracle -P2881 -pxxx --tee=/tmp/test.txt
Logging to file '/tmp/test.txt'
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3221546363
Server version: OceanBase 4.2.5.4 (r104000082025052817-2e9399047c72ee074510f39a687426e4c5649964) (Built May 28 2025 18:36:07)
Copyright (c) 2000, 2018, OceanBase and/or its affiliates. All rights reserved.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
obclient(SYS@oracle)[SYS]> select systimestamp from dual;
+-------------------------------------+
| SYSTIMESTAMP |
+-------------------------------------+
| 05-JUN-25 11.21.35.762387 PM +08:00 |
+-------------------------------------+
1 row in set (0.000 sec)
obclient(SYS@oracle)[SYS]> Bye
对应捕获的 /tmp/test.txt 文件内容如下。
[root@observer1 ~]# cat /tmp/test.txt
obclient(SYS@oracle)[SYS]> select now() from dual;
ORA-00600: internal error code, arguments: -5055, FUNCTION NOW does not exist
obclient(SYS@oracle)[SYS]> select systimestamp from dual;
+-------------------------------------+
| SYSTIMESTAMP |
+-------------------------------------+
| 05-JUN-25 11.19.15.003430 PM +08:00 |
+-------------------------------------+
1 row in set (0.001 sec)
obclient(SYS@oracle)[SYS]> notee
obclient(SYS@oracle)[SYS]>
obclient(SYS@oracle)[SYS]>
obclient(SYS@oracle)[SYS]> select systimestamp from dual;
+-------------------------------------+
| SYSTIMESTAMP |
+-------------------------------------+
| 05-JUN-25 11.20.16.074102 PM +08:00 |
+-------------------------------------+
1 row in set (0.000 sec)
obclient(SYS@oracle)[SYS]> Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3221546363
Server version: OceanBase 4.2.5.4 (r104000082025052817-2e9399047c72ee074510f39a687426e4c5649964) (Built May 28 2025 18:36:07)
Copyright (c) 2000, 2018, OceanBase and/or its affiliates. All rights reserved.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
obclient(SYS@oracle)[SYS]> select systimestamp from dual;
+-------------------------------------+
| SYSTIMESTAMP |
+-------------------------------------+
| 05-JUN-25 11.21.35.762387 PM +08:00 |
+-------------------------------------+
1 row in set (0.000 sec)
适用版本
OceanBase 数据库所有版本。
OBClient 所有版本。