54

I encountered such a problem: Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement when I tried to execute MySQL statement (Windows):

SELECT *
FROM xxxx
WHERE XXX
INTO OUTFILE 'report.csv'
    FIELDS TERMINATED BY '#'
    ENCLOSED BY '"'
    LINES TERMINATED BY '\n'

When I execute it without:

INTO OUTFILE 'report.csv'
    FIELDS TERMINATED BY '#'
    ENCLOSED BY '"'
    LINES TERMINATED BY '\n'

Then it works. Also, the same statement with INTO OUTFILE xxx actually worked before I reinstalled the MySQL server.

Anybody has ideas how to deal with this error?

Amade
  • 2,397
  • 2
  • 18
  • 39
MangooSaSa
  • 2,315
  • 3
  • 11
  • 15

4 Answers4

69

A quick answer, that doesn't require you to edit any configuration files (and works on other operating systems as well as Windows), is to just find the directory that you are allowed to save to using:

mysql> SHOW VARIABLES LIKE "secure_file_priv";
+------------------+-----------------------+
| Variable_name    | Value                 |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
+------------------+-----------------------+
1 row in set (0.06 sec)

And then make sure you use that directory in your SELECT statement's INTO OUTFILE clause:

SELECT *
FROM xxxx
WHERE XXX
INTO OUTFILE '/var/lib/mysql-files/report.csv'
    FIELDS TERMINATED BY '#'
    ENCLOSED BY '"'
    LINES TERMINATED BY '\n'

Original answer

I've had the same problem since upgrading from MySQL 5.6.25 to 5.6.26.

In my case (on Windows), looking at the MySQL56 Windows service shows me that the options/settings file that is being used when the service starts is C:\ProgramData\MySQL\MySQL Server 5.6\my.ini

On linux the two most common locations are /etc/my.cnf or /etc/mysql/my.cnf.

MySQL56 Service

Opening this file I can see that the secure-file-priv option has been added under the [mysqld] group in this new version of MySQL Server with a default value:

secure-file-priv="C:/ProgramData/MySQL/MySQL Server 5.6/Uploads"

You could comment this (if you're in a non-production environment), or experiment with changing the setting (recently I had to set secure-file-priv = "" in order to disable the default). Don't forget to restart the service after making changes.

Alternatively, you could try saving your output into the permitted folder (the location may vary depending on your installation):

SELECT *
FROM xxxx
WHERE XXX
INTO OUTFILE 'C:/ProgramData/MySQL/MySQL Server 5.6/Uploads/report.csv'
    FIELDS TERMINATED BY '#'
    ENCLOSED BY '"'
    LINES TERMINATED BY '\n'

It's more common to have comma seperate values using FIELDS TERMINATED BY ','. See below for an example (also showing a Linux path):

SELECT *
FROM table
INTO OUTFILE '/var/lib/mysql-files/report.csv'
    FIELDS TERMINATED BY ',' ENCLOSED BY '"'
    ESCAPED BY ''
    LINES TERMINATED BY '\n';
isedwards
  • 2,067
  • 16
  • 26
  • 1
    I disabled this security option by adding `secure-file-priv=` to my.ini. You can check the value of the open by using the ´show variables´ command a looking for secure_file_priv variable. For documentation about the various values, see: https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_secure_file_priv – Juha Palomäki Aug 29 '16 at 00:17
  • I wonder how I can restart the mysql service on windows 10 since I tried command like `net stop mysql` and `mysqld stop` but it does not work and finally I have to restart my computer. – LancelotHolmes Oct 28 '16 at 07:00
  • @LancelotHolmes try `net stop mysql57` – mirk Nov 01 '16 at 18:20
  • 4
    I changed `my.ini` and restarted `mysql56` and I still get this error :( – Maria Ines Parnisari Nov 15 '16 at 03:57
  • 3
    What if the value of `secure-file-priv` is null? – Christia Dec 30 '18 at 12:41
  • Did not work for me. Sheesh. They TRIED to make it hard? Basically typing `mysql -u root -p < statements` as I usually do when reading statements from a file. Now I get --help info when I hit enter xD – brat Feb 07 '21 at 18:41
  • @Christia did you got answer if secure-file-priv is null – insoftservice Mar 17 '21 at 18:51
6

If you changed my.ini and restarted mysql and you still get this error please check your file path and replace "\" to "/". I solved my proplem after replacing.

zgormez
  • 359
  • 4
  • 8
2

I had to set

C:\ProgramData\MySQL\MySQL Server 8.0/my.ini  secure-file-priv=""

When I commented line with secure-file-priv, secure-file-priv was null and I couldn't download data.

Markus
  • 1,485
  • 4
  • 18
  • 33
-3

The code above exports data without the heading columns which is weird. Here's how to do it. You have to merge the two files later though using text a editor.

SELECT column_name FROM information_schema.columns WHERE table_schema = 'my_app_db' AND table_name = 'customers' INTO OUTFILE 'C:/ProgramData/MySQL/MySQL Server 5.6/Uploads/customers_heading_cols.csv' FIELDS TERMINATED BY '' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY ',';
Svetoslav Marinov
  • 1,390
  • 12
  • 11