【分享】mysql查询结果命令行直接导出
命令格式:
前面是查询语句加上(into outfile)加上后边的输出地址
select * from xxx where xx = '' into outfile '/home/xxx.csv'
问题:
running with the --secure-file-priv option so it cannot executre this statem
如果出现上述报错,原因是Mysql配置文件规定了导出文件的默认路径,自定义路径会报错。
解决方案:
1.关闭指定,即将配置文件中的 secure-file-priv="xxx" 变成 secure-file-priv=""
2.或者直接导出到该路径 show variables like 'secure%' 找到该路径,然后导出的时候,将文件输出到这里。
--------------------------------------------------------------------------------------------------------------------------
20220608 更新,自定义secure路径
如果查询的结果为null,是无法通过该方法导出数据的。
解决方案:
vim /etc/my.cnf
# 添加如下内容,这里以/为例
secure_file_priv= "/"
可选的参数有
secure_file_priv=null 不允许csv文件的导入导出
secure_file_priv=xxx csv文件导入导出到某路径
secure_file_priv=/ csv文件可导入到任意路径
systemctl restart mysqld
如果是docker 版本,可能路径为 /etc/mysql/my.cnf