MySQL exporting INTO OUTFILE --secure-file-priv er

2020-02-25 23:21发布

问题:

I'm trying to run the following on a MySQL database:

SELECT * FROM mysql.db
INTO OUTFILE "C:\ProgramData\MySQL\MySQL Server 5.7\Uploads\db.csv"
FIELDS TERMINATED BY '|'
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

I get the error:

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

When I run the following:

mysql> SELECT @@secure_file_priv;

I get:

+------------------------------------------------+
| @@secure_file_priv                             |
+------------------------------------------------+
| C:\ProgramData\MySQL\MySQL Server 5.7\Uploads\ |
+------------------------------------------------+

So why is it not exporting the file even though I am using the set --secure-file-priv location?

I am used to MSSQL and new to MySQL.

回答1:

It is important to use path location mentioned in:

mysql> SELECT @@secure_file_priv;

If you will use customized path location, you will still get this error. As mentioned by Lateralus, don't forget to change path to forward slashes.



回答2:

Argh. It was a freakin' typo, my \'s should have been /'s

So my query is now this:

SELECT * FROM   mysql.db INTO OUTFILE "C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/db_pipe.csv" FIELDS TERMINATED BY '|' ENCLOSED BY '"'  LINES TERMINATED BY '\n';


回答3:

This worked for me. It needs double backslash and if you are trying any tools in between to connect to mysql simply add escape chars.

SELECT * 
      INTO OUTFILE 'C:\\\\ProgramData\\\\MySQL\\\\MySQL Server 8.0\\\\Uploads\\\\employees.txt'
        FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
        LINES TERMINATED BY '\n'
      FROM employees;


回答4:

In my case (Windows):

  1. In my.ini, set secure_file_priv=""

  2. Use double back slash in the path like below:

SELECT description, comment FROM usecase
INTO OUTFILE 'C:\\tmp\\usecase0.csv' 
FIELDS ENCLOSED BY '"' 
TERMINATED BY ';' 
ESCAPED BY '"' 
LINES TERMINATED BY '\r\n';