使用SELECT ... INTO OUTFILE导出mysql数据

select ...into outfile语法是mysql提供的一种数据导出的方案,支持自定义导出数据格式。
例如:将test表的所有数据全部导出到test.txt文件。

select * from test  into outfile '/tmp/test.txt';

更常用的方法是将表数据导出成csv格式,方便数据的查看以及重新导入

select a,b,c from test into outfile '/tmp/test.text' fields terminated by ',' lines terminated by '\n' ;

使用逗号分隔,使用\n作为换行符。这里和load data 导入数据相对应。

如果使用 into outfile导出数据的时候出现以下错误:

The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

使用

show variables like ‘%secure%’;

查看 secure-file-priv 当前的值,导出的数据必须是这个值的指定路径才可以导出。

posted @ 2022-12-16 21:04  华小电  阅读(387)  评论(0编辑  收藏  举报