linux服务器上导出mysql的查询结果

运营需要一些数据,要求又紧急,写个程序貌似来不及,只好登录到服务器执行SQL。

首先连接到数据库

mysql -u 用户名 -p [-h 数据库ip地址] [-P 端口号] [数据库名]

会提示你输入密码

mysql> select xxx from table_name into outfile '/tmp/test.xls';

显示一个错误,度娘后发现没有输出到mysql配置的文件夹内
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

mysql> show variables like '%secure%';
或
mysql> SHOW VARIABLES LIKE "secure_file_priv";
+--------------------------+-----------------------+
| secure_file_priv         | /var/lib/mysql-files/ |
+--------------------------+-----------------------+

mysql> select xxx from table_name into outfile '/var/lib/mysql-files/test.xls';

显示导出成功,然后到目录下一看,文件并不存在

运营小哥又在催了,怎么办?

放大招,将查询结果都导出到指定文件吧

mysql> pager cat > /var/lib/mysql-files/test.txt ;

mysql>select xxx from table_name;

文件果然生成了。这个时候,查询结果也不会显示在控制台。要是想要显示结果,可以取消导出到文件,

 mysql>nopager;

暂时算是完成了小哥的任务。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注