Disable secure priv for data loading on MySQL

2019-01-22 21:02发布

问题:

I'm running MySQL 5.7 on a Windows 10 machine. I've read through all the SO threads on this topic and still haven't figured out how to get my data to load and get past this error:

Error Code: 1290. The MySQL server is running with the --secure-file-priv 
option so it cannot execute this statement

I have 1) checked the settings to change them to be able to load from the directory in which I've saved my dataset, 2) opened up MySQL as administrator and checked the command line and have confirmed that the secure file does indeed point to my directory, 3) and confirmed in the init file that it's pointing to the correct directory containing my file. I tried changing the location of the dataset so it would be in a new folder and confirmed it had been moved there with the above methods, and it still did not work.

Any and all help would be welcome, thank you.

回答1:

I can't reproduce the problem.

mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.13    |
+-----------+
1 row in set (0,00 sec)

mysql> SELECT @@GLOBAL.secure_file_priv;
+---------------------------+
| @@GLOBAL.secure_file_priv |
+---------------------------+
| NULL                      |
+---------------------------+
1 row in set (0,00 sec)

-- USE ...

mysql> LOAD DATA INFILE '/var/lib/mysql-files/myfile.csv'
    -> INTO TABLE `test_files`
    -> COLUMNS TERMINATED BY ',' ENCLOSED BY '\"'
    -> LINES TERMINATED BY '\n';
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv
option so it cannot execute this statement

Change file: /etc/mysql/my.cnf

[mysqld]
.
.
.
secure_file_priv=/var/lib/mysql-files/
.
.
.

Restart MySQL.

mysql> SELECT @@GLOBAL.secure_file_priv;
+---------------------------+
| @@GLOBAL.secure_file_priv |
+---------------------------+
| /var/lib/mysql-files/     |
+---------------------------+
1 row in set (0,00 sec)

mysql> LOAD DATA INFILE '/var/lib/mysql-files/myfile.csv'
    -> INTO TABLE `test_files`
    -> COLUMNS TERMINATED BY ',' ENCLOSED BY '\"'
    -> LINES TERMINATED BY '\n';
Query OK, 3 rows affected (0,00 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0

See 6.1.4 Server System Variables :: secure_file_priv



回答2:

On MACOSX add .my.cnf to your home directory with content:

[mysqld_safe]
[mysqld]
secure_file_priv=""

https://github.com/Homebrew/homebrew-versions/issues/1552



回答3:

This works in MacOs Sierra 10.12.6:

use the command

mysql --help | more

and look for a line where it is written:

Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf 
~/.my.cnf

In my case I tried to create a my.cnf in the home directory but it did not work. The only solution I found is to create the file in the folder etc with

sudo vim /etc/my.cnf

and put inside it

[mysqld_safe]
[mysqld]
secure-file-priv = ""

Then you can check that everything works with

select @@GLOBAL.secure_file_priv;

inside mysql and check that the value is empty and different from NULL.

Finally save files in the directory /tmp and then move them in the directory you want. Alternatively (but possibly unsafe) use

chmod 1777 dir

where dir is the name of the directory in which you are writing the files.



回答4:

  1. Check OS privileges on directory you are importing from.
  2. When trying to import your data via "CVS using LOAD DATA" select use local option.


回答5:

Note that under Windows setting the 'secure_file_priv' to a different path or disabling it altogether by setting it to:

secure_file_priv=""

may not work if the MySQL service is running on a low-privilege account (default 5.7 installation). You can change that by selecting the "Local System account" in the Services under Properties -> Log On.