LOAD DATA INFILE Error Code : 13
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.csv'
INTO TABLE table_temp
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\\r \\n'
(sku, qty);
Error Code : 13 Can't get stat of '/httpdocs/.../.../testFile.csv' (Errcode: 2)
a. The database userlogin has all the grant priviliges.
CREATE USER 'userName'@'%' IDENTIFIED BY '************';
GRANT ALL PRIVILEGES ON * . * TO 'userName'@'%' IDENTIFIED BY '************' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;
GRANT ALL PRIVILEGES ON `userName\_%` . * TO 'userName'@'%';
b. I have also set the file and folder permission to chmod 777 (rwxrwxrwx) using FTP Tool
I know that this post is old, but this still comes up in search results. I couldn't find the solution to this problem online, so I ended up figuring it out myself. If you're using Ubuntu, then there is a program called "Apparmor" that is preventing MySQL from seeing the file. Here's what you need to do if you want MySQL to be able to read files from the "tmp" directory:
sudo vim /etc/apparmor.d/usr.sbin.mysqld
Once you are in the file, you're going to see a bunch of directories that MySQL can use. Add the line /tmp/** rwk
to the file (I am not sure that it matters where, but here is a sample of where I put it):
/etc/mysql/*.pem r,
/etc/mysql/conf.d/ r,
/etc/mysql/conf.d/* r,
/etc/mysql/*.cnf r,
/usr/lib/mysql/plugin/ r,
/usr/lib/mysql/plugin/*.so* mr,
/usr/sbin/mysqld mr,
/usr/share/mysql/** r,
/var/log/mysql.log rw,
/var/log/mysql.err rw,
/var/lib/mysql/ r,
/var/lib/mysql/** rwk,
/tmp/** rwk,
/var/log/mysql/ r,
/var/log/mysql/* rw,
/var/run/mysqld/mysqld.pid w,
/var/run/mysqld/mysqld.sock w,
/run/mysqld/mysqld.pid w,
/run/mysqld/mysqld.sock w,
Now all you need to do is reload Apparmor:
sudo /etc/init.d/apparmor reload
Note I used "vim", but substitute that with whatever your favorite text editor is that you know how to use.
Adding the keyword 'LOCAL' to my query worked for me:
LOAD DATA LOCAL INFILE 'file_name' INTO TABLE table_name
A detailed description of the keyword can be found here.
This is normally a file access permissions issue but I see your already addressing that in point b, but it's worth going over just in case. Another option is to use LOAD DATA LOCAL INFILE which gets past a lot of these issues of file access permissions. To use this method though you need to copy the file locally (in the mysql folder is best) first. •If LOCAL is specified, the file is read by the client program on the client host and sent to the server.
Insufficient Directory Permissions
The error in this example has resulted because the file you are trying to import is not in a directory which is readable by the user the MySql server is running as. Note that all the parent directories of the directory the is in need to be readable by the MySql user for this to work. Saving the file to /tmp will usually work as this is usually readable (and writable) by all users. The error code number is 13. Source
EDIT:
Remember you will need permissions on not just the folder the holds the file but also the upper directories.
Example from this post on the MySql Forums.
If your file was contained within the following strucutre: /tmp/imports/site1/data.file
you would need (I think, 755 worked) r+x for 'other' on these directories:
/tmp
/tmp/imports
As well as the main two:
/tmp/imports/site1
/tmp/imports/site1/data.file
You need the file and directory to be world-readable. Apologies if you've already tried this method but it may be worth retracing your steps, never hurts to double check.
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