mac下mysql解决导出csv文件secure_file_priv报错的解决方案

1. mysql导出数据到csv文件

在业务场景中,经常需要将数据库中数据导出到本地文件,可采用如下命令写到本地csv文件

select * from table
into outfile '/tmp/' #写到指定文件
fields terminated by ',' #字段间的分隔符
optionally enclosed by '"' #字段的包围符
lines terminated by '\n'; #行与行的分隔符

默认安装的mysql会产生如下报错

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

这是由于mysql默认secure_file_priv为null,
mac下mysql解决导出csv文件secure_file_priv报错的解决方案_第1张图片
即将数据保护起来,不允许与本地文件进行交互,我们来看一下官方文档对secure_file_priv的解释:
mac下mysql解决导出csv文件secure_file_priv报错的解决方案_第2张图片
可以看到secure_file_priv不同值对应的配置

含义
null mysql服务器限制导入与导出
empty mysql服务器对导入导出不限制,当然这会产生一些安全问题
PATH of directory 允许从指定的路径导入导出数据

2. mac下解决该问题

实际上,只要对mysql进行一下参数配置就可以了,配置文件为my.cnf,貌似mac版本存在某种问题,无法找到my.cnf这个文件,我们需要自行配置,首先看一下mysql读取my.cnf的顺序。
mysql读取my.cnf顺序

划重点,解决secure_file_priv的操作步骤!!!

  1. 首先停止mac上的mysql服务
  2. 系统设置-mysql-停止服务
    mac下mysql解决导出csv文件secure_file_priv报错的解决方案_第3张图片
  3. 编写配置文件
sudo vim /etc/my.cnf

添加如下

[mysqld]
secure_file_priv='/path/to/destination/'
  1. 再次打开mysql服务,终端登录
    show variables like '%secure_file_priv%';
    mac下mysql解决导出csv文件secure_file_priv报错的解决方案_第4张图片
  2. 登录mysql,再次执行导出操作,需要确保与secure_file_priv路径一致。
    在这里插入图片描述
    在这里插入图片描述

你可能感兴趣的:(mysql)