mysql export select excel

1、mysql连接+将查询结果输出到文件

mysql -hxx -uxx -p -e "query statement" db > file 

  -h:后面跟的是链接的host(主机)

  -u:后面跟的是用户名

  -p:后面跟的是密码

  db:你要查询的数据库

  file:你要写入的文件,绝对路径

例:

mysql -h127.0.0.1 -uroot -p123 -e "select * from edu_iclass_areas" test > /Users/zhengcanrui/WORK/test/test.xls

2、mysql连接 和 将查询结果输出到数据库分开执行

> mysql -hxxx -uxx -pxx 
>> select * from table into outfile 'xxx.txt'; 

  -h/-u/-p 的参数都没的内容和上面一致, xxx.txt  是要输出的文件路径及其名称。

例:

> mysql -h127.0.0.1 -uroot -p;
>> select * from tableName where goods_sku like 'N190%' into  outfile '/tmp/n190.xls';

此时可能会遇到如下问题:

ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

查看secure配置:

mysql> show variables like '%secure%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| require_secure_transport | OFF   |
| secure_file_priv         | NULL  |
+--------------------------+-------+
2 rows in set (0.00 sec)

修改此处配置即可在mysql数据库内导出文件。

猜你喜欢

转载自blog.csdn.net/u012160319/article/details/82021641