当使用命令行终端执行查询时,如何自动保存 mysql 查询?

当使用命令行终端执行查询时,如何自动保存 mysql 查询?

我在这里尝试的是,我想将 mysql 查询及其输出保存在文件中,并显示在 ubuntu 终端上。我的方法是这样的:

$ mysql -u user -pmysql |& tee day1.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
use AUDIT;
select * from CUSTOMERS;

但是直到我按下 ctrl + D 它才显示输出。它似乎将我的查询发送到 mysql 服务器。我想在终端中看到查询的输出,有什么办法吗?

答案1

mysql的输出不是终端时,它假定非交互式使用,并等待输入结束后再执行。您可以使用选项--tee(或其内部的相关tee命令mysql)。从man mysql

--tee=file_name

Append a copy of output to the given file. This option works only in interactive mode.
the section called “MYSQL COMMANDS”, discusses tee files further.

...

tee [file_name], \T [file_name]

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.

所以:

mysql -u user -pmysql --tee=day1.sql

相关内容