命令记不住

登陆

mysql5.7 提示 --secure-file-priv 选项报错解决

mysql5.7提示--secure-file-priv选项报错解决

1.系统环境

CentOS Linux release 7.4.1708 (Core)
mysql 5.7.23

2.错误描述

执行数据导入时,出现1290错误:

mysql> load data infile '/mnt/test/20190220/test_eventHistory_2019022000_2019022023.txt' into table event_history_201902(json_str);
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

3.问题分析

简述下错误信息,mysql服务运行时启用了--secure-file-priv option参数,因此这个语句无法执行。查阅官方文档,secure_file_priv 这个参数用于限制数据的导入和导出,例如LOAD DATA
或者 SELECT ... INTO OUTFILE 以及 LOAD_FILE()函数,要想执行以上操作用户需要具有FILE权限。
secure_file_priv可以设置为如下:
如果为空,则此参数没有作用,即不做限制
如果设置为目录,则数据的导入导出就只会限制在此目录中,并且这个目录必须事先存在,服务器并不会创建它
如果设为NULL,服务器会禁止数据的导入导出操作。

查看一下当前系统secure_file_priv 的值:

mysql> show global variables like '%secure%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| require_secure_transport | OFF   |
| secure_auth              | ON    |
| secure_file_priv         | NULL  |
+--------------------------+-------+
3 rows in set (0.01 sec)

值为NULL, 由此可知服务器会禁止数据的导入导出操作,尝试修改此参数:

mysql> set global secure_file_priv='';
ERROR 1238 (HY000): Variable 'secure_file_priv' is a read only variable

这是一个只读参数,不能使用set global来修改

4.解决办法

在mysql配置文件/etc/my.cnf中[mysqld]配置段添加如下配置

[mysqld]

secure_file_priv=''

重启mysql并再次查看secure_file_priv的值

# /etc/init.d/mysqld restart 
#
mysql> show global variables like '%secure%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| require_secure_transport | OFF   |
| secure_auth              | ON    |
| secure_file_priv         |       |
+--------------------------+-------+
3 rows in set (0.00 sec)

再次执行导入操作,成功

db01-[(none)]>SELECT CONCAT("alter table ",table_name," discard tablespace;") 
    -> FROM information_schema.tables  WHERE table_schema='world' INTO OUTFILE '/tmp/discard.sql';
Query OK, 3 rows affected (0.00 sec)