【MySQL】将执行结果输出到文件

Posted by 西维蜀黍 on 2020-07-06, Last Modified on 2021-09-21

Approach 1

mysql> select count(1) from table  into outfile '/tmp/test.log';

Query OK, 31 rows affected (0.00 sec)

在目录/tmp/下会产生文件test.xlog

遇到的问题:

mysql> select count(1) from table into outfile '/tmp/test.log';
ERROR 1 (HY000): Can't create/write to file '/tmp/test.log' (Errcode: 13)

原因:mysql User没有向/tmp/下写的权限。

Approach 2

# 之后的所有查询结果都自动写入/tmp/test.log
mysql> pager cat > /tmp/test.log ;
PAGER set to 'cat > /tmp/test.log'

mysql> select * from table ;
30 rows in set (0.59 sec)
# 在窗口内不再显示查询结果

Approach 3

$ mysql -h 127.0.0.1 -u root -p XXXX -P 3306 -e "select * from table" > /tmp/test.log

Reference