In my remote MySQL, when I try to execute this query, I am getting the MySQL Error Code : 13.
Query -
LOAD DATA INFILE
\'/httpdocs/.../.../testFile.
local_infile
is on SHOW VARIABLES LIKE 'local_infile';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| local_infile | ON |
+---------------+-------+
If not, restart mysqld
with:
sudo ./mysqld_safe --local-infile
change you csv file to /tmp
folder so that it mysqls can read it.
import to db
mysql> LOAD DATA INFILE '/tmp/a.csv' INTO TABLE table_a FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 ROWS;
Query OK, 18 rows affected (0.17 sec)
Records: 18 Deleted: 0 Skipped: 0 Warnings: 0
OLD:
LOAD DATA INFILE '/home/root12/Downloads/task1.csv' INTO TABLE test.task FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 ROWS;
File '/home/root12/Downloads/task1.csv' not found (Errcode: 13 - Permission denied)
NEW WORKING FOR ME:
LOAD DATA LOCAL INFILE '/home/root12/Downloads/task1.csv' INTO TABLE test.task FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 ROWS;
Query OK, 500 rows affected, 284 warnings (1.24 sec)
Query OK, 5000 rows affected, 2846 warnings (1.24 sec)
If you are using Fedora/RHEL/CentOO you can disable temporarily SELinux:
setenforce 0
Load your data and then enable it back:
setenforce 1
I had a lot of trouble to fix this, there are two things to do :
update file /etc/mysql/my.cnf
with local-infile =1
at two locations after [mysql]
paragraphs (this allows to use LOCAL INFILE sql command).
update via sudo gedit /etc/apparmor.d/usr.sbin.mysqld
so apparmor will allow you to write into the special files of /var/www/
by adding the lines:
/name of your directory/ r,
/name of your directory/* rw,
The name of your directory can be as /var/www/toto/
(this allows to use the directory where your files are located).
There is one property in mysql configuration file under section [mysqld]
with name - tmpdir
for example:
tmpdir = c:/temp (Windows) or tmpdir = /tmp (Linux)
and LOAD DATA INFILE
command can perform read and write on this location only.
so if you put your file at that specified location then LOAD DATA INFILE
can read/write any file easily.
One more solution
we can import data by following command too
load data local infile
In this case there is no need to move file to tmpdir, you can give the absolute path of file, but to execute this command, you need to change one flag value. The flag is
--local-infile
you can change its value by command prompt while getting access of mysql
mysql -u username -p --local-infile=1
Cheers