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 JOI
secure-file-priv = ""
line at the endsystemctl stop mysqld
systemctl start mysqld
It will now allow you to import and export the data.
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
andSELECT ... INTO OUTFILE
statements and theLOAD_FILE()
function. These operations are permitted only to users who have theFILE
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.
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.
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
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
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