开发要求把表中数据导出到csv,以下代码之前在mysql 5.6运行正常
select * from user into outfile '/tmp/user.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\r\n';
-- fields terminated by 字段分隔符
-- optionally enclosed by 字段包围符(数值型字段无效)
-- lines terminated by 行间分隔符
但在5.7运行时遇到以下报错,无法执行
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
查看官方文档,secure_file_priv参数用于限制LOAD DATA, SELECT …OUTFILE, LOAD_FILE() 等命令,操作的文件应该导入导出到哪个指定目录。可选值如下:
- 为 NULL(默认):mysql不允许导入或导出
- 为 具体目录(例如/tmp):只能在指定目录执行导入导出,其他目录不能执行
- 没有值:不限制,可在任意目录导入导出
查看我们的 secure_file_priv ,发现为NULL。如果是具体目录,只需将into outfile 后路径指定到该目录即可。
mysql> show global variables like '%secure_file_priv%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| secure_file_priv | NULL |
+------------------+-------+
1 row in set (0.00 sec)
注意secure_file_priv 参数是只读参数,不能使用set global命令修改。
mysql> set global secure_file_priv='';
ERROR 1238 (HY000): Variable 'secure_file_priv' is a read only variable
解决方法
修改参数文件,加入以下语句,约停机时间重启mysql。
secure_file_priv=''
查看secure_file_priv修改后的值
mysql> show global variables like '%secure_file_priv%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| secure_file_priv | |
+------------------+-------+
1 row in set (0.00 sec)
修改后再次执行,成功导出。
mysql> select * from user into outfile '/tmp/user.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\r\n';
Query OK, 15 rows affected (0.00 sec)
参考
https://blog.csdn.net/fdipzone/article/details/78634992