本文已参与「新人创作礼」活动,一起开启掘金创作之路。
有时我们需要执行大量的 SQL 命令,并将执行结果保存到本地,方便后续分析处理。如何实现这一诉求呢?方法比较多,本文简单采用 tee 命令实现。
Linux tee 命令用于读取标准输入的数据,并将其内容输出成文件。tee 指令会从标准输入设备读取数据,将其内容输出到标准输出设备,同时保存成文件,其语法如下所示。
tee [-ai][--help][--version][文件...]
参数详解:
-a 或 --append:附加到既有文件的后面,而非覆盖它。
-i 或 --ignore-interrupts:忽略中断信号。
--help:在线帮助。
--version:显示版本信息。
MySQL 扩展了 tee 命令,采用 tee 命令将远程执行结果保存到本地,具体可以采用以下 2 种形式。
一、在连接数据库时使用 tee
第一种命令行的方式是在连接 MySQL 时,通过 --tee=[your file] 参数将结果记录在本地磁盘文件,操作示例如下所示(本地采用 PowerShell 连接数据库,为避免隐私,以下测试数据中的数据库 IP、端口、数据库名及表等数据均为虚构数据)。
PS C:\Users\testuser> mysql -h 10.21.24.15 -P 60008 testmgmdb --tee=D:/result.txt -u testuser -p
Logging to file 'D:/result.txt'
Enter password: ***********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 204762
Server version: 5.7.23-mysql-2.4.1-log MYSQL Server
Copyright (c) 2000, 2014, 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> select count(1) from tbl_mgm_menu;
+----------+
| count(1) |
+----------+
| 19498 |
+----------+
1 row in set (0.01 sec)
mysql> select count(1) from tbl_mgm_campus;
+----------+
| count(1) |
+----------+
| 4803 |
+----------+
1 row in set (0.00 sec)
mysql> quit
Bye
查看 D:/result.txt 文件内容,如下所示。
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 204762
Server version: 5.7.23-mysql-2.4.1-log MYSQL Server
Copyright (c) 2000, 2014, 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> select count(1) from tbl_mgm_menu;
+----------+
| count(1) |
+----------+
| 19498 |
+----------+
1 row in set (0.01 sec)
mysql> select count(1) from tbl_mgm_campus;
+----------+
| count(1) |
+----------+
| 4803 |
+----------+
1 row in set (0.00 sec)
mysql> quit
操作 MySQL 的全部过程均被记录到指定文件中。
二、在连接上数据库后使用
第二种命令行的方式是在连接上 MySQL 后使用,开启 tee 记录功能和退出 tee 记录功能的操作示例如下所示。
mysql> tee D:/result.txt
Logging to file 'D:/result.txt'
mysql> -- 查询菜单表总数据量
mysql> select count(1) from tbl_mgm_menu;
+----------+
| count(1) |
+----------+
| 19498 |
+----------+
1 row in set (0.01 sec)
mysql> -- 查询校园表总数据量
mysql> select count(1) from tbl_mgm_campus;
+----------+
| count(1) |
+----------+
| 4803 |
+----------+
1 row in set (0.00 sec)
mysql> -- 当不想再记录后续操作结果时,可以使用 notee 命令,这个命令后面的操作将不会再被记录
mysql> notee;
Outfile disabled.
mysql> quit
Bye
查看输出文件,内容如下所示。
mysql> -- 查询菜单表总数据量
mysql> select count(1) from tbl_mgm_menu;
+----------+
| count(1) |
+----------+
| 19498 |
+----------+
1 row in set (0.01 sec)
mysql> -- 查询校园表总数据量
mysql> select count(1) from tbl_mgm_campus;
+----------+
| count(1) |
+----------+
| 4803 |
+----------+
1 row in set (0.00 sec)
mysql> -- 当不想再记录后续操作结果时,可以使用 notee 命令,这个命令后面的操作将不会再被记录
mysql> notee;
结果被成功记录到指定文件。