41

I am trying to write the results of MySQL script to a text file using the following code in my script.

SELECT p.title, p.content, c.name FROM post p
LEFT JOIN category c ON p.category_id=c.id
INTO OUTFILE 'D:\MySql\mysqlTest.txt';

However, I am getting the following

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

How do I solve this?

random
  • 9,774
  • 10
  • 66
  • 83
Adhil
  • 1,678
  • 3
  • 20
  • 31
  • In case it can help you... https://stackoverflow.com/questions/14416379/errcode-13-select-into-outfile-issue/53760735#53760735 – Petrus Dec 13 '18 at 11:56
  • I posted a possible answer on another stackoverflow post: https://stackoverflow.com/questions/14416379/errcode-13-select-into-outfile-issue/53760735#53760735 – Petrus Dec 14 '18 at 08:18

11 Answers11

63

Ubuntu 16.04 (EASY): Find out where you are allowed to write

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

Then, just write there

mysql> SELECT * FROM train INTO OUTFILE '/var/lib/mysql-files/test.csv' FIELDS TERMINATED BY ',';
Query OK, 992931 rows affected (1.65 sec)

mysql>

Mac OSX: Mysql installed via MAMP

Find out where you are allowed to write

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

NULL means you're screwed so you have to create the file "~/.my.cnf"

Enable read/write for MySQL installed via MAMP (on Mac):

  1. open "MAMP" use spotlight
  2. click "Stop Servers"
  3. edit ~/.my.cnf (using vi or your favorite editor) and add the following lines:

    $ vi ~/.my.cnf

[mysqld_safe]
[mysqld]
secure_file_priv="/Users/russian_spy/"
  1. click "Start Servers" (in MAMP window)

Now check if it works:

a. start mysql (default MAMP user is root, password is also root)

$ /Applications/MAMP/Library/bin/mysql -u root -p 

b. in mysql look at the white-listed paths

mysql> SELECT @@GLOBAL.secure_file_priv;
+---------------------------+
| @@GLOBAL.secure_file_priv |
+---------------------------+
| /Users/russian_spy/          |
+---------------------------+
1 row in set (0.00 sec)

c. Finally, test by exporting a table train into a CSV file

mysql> SELECT * FROM train INTO OUTFILE '/Users/russian_spy/test.csv' FIELDS TERMINATED BY ',';
Query OK, 992931 rows affected (1.65 sec)

mysql>
russian_spy
  • 6,465
  • 4
  • 30
  • 26
  • 2
    Or just set the `secure_file_priv = ""` in `~/.my.cnf` if you wish to export to any directory on your local workstation. – demisx Mar 20 '19 at 15:29
  • Thanks for your helpful answer! If anyone like me was able to export the file with the help of this answer but couldn't open it due to secure_file_priv, you may use "sudo cp -rf dir_cur_path dir_distenation_path" to use the file. – Esraa Abdelmaksoud Aug 21 '23 at 00:14
20
  1. Edit the (/etc/my.cnf file for CentOS) or (my.ini file for Windows)
  2. Add secure-file-priv = "" line at the end
  3. Stop mysql service using systemctl stop mysqld
  4. Restart it using systemctl start mysqld

It will now allow you to import and export the data.

Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85
Kanchan Waikar
  • 201
  • 2
  • 2
10

Replace "\" to "/" in your file path.

Like this:

INTO OUTFILE 'D:/MySql/mysqlTest.txt';
zgormez
  • 429
  • 5
  • 9
  • Worked on Windows. MySQL gave no indication the path separator was the problem! The error message was only, "Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement" even though the output directory for `OUTFILE` was in `secure-file-priv` (configured in `my.ini`) and the MySQL user had full privileges. – Tyler Jun 26 '23 at 19:37
8

You cannot export data as it is configured in mysql config files. Open my.cnf config file and check.

Quote from MySQL doc

This variable is used to limit the effect of data import and export operations, such as those performed by the LOAD DATA and SELECT ... INTO OUTFILE statements and the LOAD_FILE() function. These operations are permitted only to users who have the FILE privilege.

secure_file_priv may be set as follows:

  • If empty, the variable has no effect.

  • If set to the name of a directory, the server limits import and export operations to work only with files in that directory. The directory must exist; the server will not create it.

  • If set to NULL, the server disables import and export operations. This value is permitted as of MySQL 5.7.6.

(An empty value is the default, or it can be explicitly specified in my.cnf as secure_file_priv="". A NULL value can be set with secure_file_priv=NULL.)

So, if you want to export data, then you need to comment this option and restart mysql server. Then you will be able to export.

miken32
  • 42,008
  • 16
  • 111
  • 154
Naruto
  • 4,221
  • 1
  • 21
  • 32
  • 2
    Read http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html for more server system variable of mysql. There are quite intersting things that can be done using these variables. – Naruto Dec 05 '15 at 08:00
  • 1
    I received the same error message on linux for not having file permissions correct for mysql (which was misleading). Make sure to allow mysql user read/write/execute as applicable, including for any parent folders. – Justin Oct 16 '17 at 03:13
5

Just create a file /etc/my.cnf with the following content

[mysqld]
secure_file_priv            = ''

You can use this oneliner:

echo "[mysqld]\nsecure_file_priv\t\t= ''\n" | sudo tee /etc/my.cnf

And then restart mysql. If brew was used to install the mysql run the following command:

brew services restart mysql
koleS
  • 1,263
  • 6
  • 30
  • 46
4

FOR MAC OS, if installed via HOMEBREW:

Edit my.cnf PATH: /usr/local/etc/my.cnf

COPY this:

# Default Homebrew MySQL server config
[mysqld]
# Only allow connections from localhost
bind-address = 0.0.0.0
secure-file-priv = ''

SAVE

Explanation: Secure_file_prive = NULL -- Limit mysqld not allowed to import and export Secure_file_priv = '/tmp/' -- Limit mysqld import and export can only occur in /tmp/ directory Secure_file_priv = '' -- does not restrict the import of mysqld

2

This is the example of my SQL that worked perfectly in WAMP SERVER (windows):

SELECT display_name,user_email,user_registered FROM test_wp_users
ORDER BY user_registered ASC
INTO OUTFILE 'C:/wamp64/tmp/usersbyemail.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

PS: Note that bars in path should be left-to-right to work perfectly in MYSQL.

erovere
  • 65
  • 3
  • Because default value is platform specific and it is empty on Windows (means it can write anywhere), see https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_secure_file_priv – endo64 Jan 09 '19 at 07:29
1

In my my.ini I only had

# Secure File Priv.

and I tried to put:

# Secure File Priv.
secure-file-priv = ""

and

# Secure File Priv.
secure-file-priv = NULL

without making it work.

I finally deleted the line and left alone:

secure-file-priv = ""

Working correctly.

meisaa
  • 11
  • 1
0

That's because secure_file_priv is set to NULL

mysql> show variables like secure_file_priv;

| secure_file_priv | NULL  |

You must stop mysql server

shell>/usr/local/mysql/support-files/mysql.server stop

Then restart mysql with the option defining where you want your files to be written to, for example to /tmp

shell>/usr/local/mysql/support-files/mysql.server start --secure-file-priv=/tmp

Then in mysql terminal you should see where your files can now be written to

mysql> show variables like secure_file_priv;

| secure_file_priv | /private/tmp/ |

On Mac you can find this folder by using Go To Folder /private/tmp in Finder

Djensen
  • 1,337
  • 1
  • 22
  • 32
imoll
  • 1
0

Go to C:\ProgramData\MySQL\MySQL Server 8.0 and you will find my.ini file. Open my.ini file in text editor (recommended notepad++) and then search for secure_file_priv and then replace will secure_file_priv="" and then save the file.

and restart your laptop.

Amit Jajoo
  • 220
  • 3
  • 7
0

For someone who is trying to export - from dbeaver.

You can right-click the query, and get the exact output of custom query as export.

dbeaver export SQL query

No need for another conf file.

Paul Bradbury
  • 482
  • 6
  • 8