1. 注意txt文本一定要是utf-8编码的,不然会报错ERROR 1300 (HY000): Invalid utf8 character string: ‘‘
2. 然后在mysql中输入:show variables like ‘%secure%‘;
<ignore_js_op>

用来查看mysql导入导出目录必须存放的位置。可以在/etc的mysql配置里面更改。
这里看到的是/var/lib/mysql_files/。
所以要把文件放在这个目录下,不然导入不了。
如果是root登录的数据库,文件的所有者和所有组最好也改成root吧。没有实验过不知道会不会出错。我是用的root.- -
3. 在mysql输入load data infile ‘/var/lib/mysql-files/你的文件名.txt‘ into table info fields terminatedby ‘&‘;
txt里面的数据格式 广东省&广州市&VIP&15875097133&2018-09-02
这时候就导入啦,如果太多文件的话就写个python脚本来导入。
4. 检查一下:
<ignore_js_op>

5. 检查无误后导出到txt文档。
select distinctnum,province,city,type,time from info order by num desc into outfile‘/var/lib/mysql-files/result.txt‘;
注意文件路径也要是/var/lib/mysql-files/。不然会报错ERROR 1290 (HY000): The MySQL server is running with the--secure-file-priv option so it cannot execute
this statement。
还有就是distinct必须写在开头的第一位。
导出来的结果是这样的。
<ignore_js_op>

如果想要导出的文件和原文件保持同样的格式:
select distinctnum,province,city,type,time from info order by num desc into outfile‘/var/lib/mysql-files/ddd.txt‘ FIELDS TERMINATED BY ‘&‘;
<ignore_js_op>

更多技术资讯可关注:gzitcast
txt导入mysql和导出的操作
原文:https://www.cnblogs.com/heimaguangzhou/p/11678901.html