Home » Mysql » LOAD DATA INFILE Error Code : 13

LOAD DATA INFILE Error Code : 13

Posted by: admin November 1, 2017 Leave a comment

Questions:

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

Answers:

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.

Questions:
Answers:

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.

Questions:
Answers:

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.

Questions:
Answers:

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

Questions:
Answers:

Error 13 is nothing but the permission issues.
Even i had the same issue and was unable to load data to mysql table and then resolved the issue myself.

Here’s the solution:

Bydefault the

–local-infile is set to value 0

, inorder to use LOAD DATA LOCAL INFILE it must be enabled.

So Start mySQL like this :

mysql -u username -p –local-infile

This will make LOCAL INFILE enabled during startup and thus there wont be any issues using it !

Questions:
Answers:

I use Ubuntu 12.04, I had to create the table, then do one of these:

Using local gives an error (mysql 5.5):

> LOAD DATA LOCAL INFILE "file.csv" INTO table inverter FIELDS TERMINATED BY ',';
ERROR 1148 (42000): The used command is not allowed with this MySQL version

LOAD DATA INFILE command is disabled by default for security reasons, re-enable it here and it should work: https://stackoverflow.com/a/16286112/445131

You can import the csv-file using mysqlimport:

mysqlimport -u root -p --fields-terminated-by=',' --local dbname tablename.csv

Note the csv-file must have same name before the extension as the table.

Questions:
Answers:

If you are using Fedora/RHEL/CentOO you can disable temporarily SELinux:

setenforce 0

Load your data and then enable it back:

setenforce 1

Questions:
Answers:

for Ubuntu users

I’m running mysql 5.6.28 on Ubuntu 15.10 and I just ran into the exact same problem, I had all the necessary flags in my.cnf
tmpdir = /tmp
local-infile=1
restarted mysql and I would still get
LOAD DATA INFILE Error Code : 13

Just like Nelson mentionned the issue was “apparmor”, sort of patronising mysql about permissions, I then found the solution thanks to this quick & easy tutorial.

basically, assuming your tmp dir would be /tmp :

Add new tmpdir entries to /etc/apparmor.d/local/usr.sbin.mysqld

sudo nano /etc/apparmor.d/local/usr.sbin.mysqld

*add this

/tmp/ r,
/mnt/foo/tmp/** rw,

Reload AppArmor

sudo service apparmor reload

Restart MySQL

sudo service mysql restart

I hope that’ll help few Ubuntu-ers

Questions:
Answers:

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).

Questions:
Answers:

I have experienced same problem and applied the solutions above.

First of all my test environment are as follows

  • Ubuntu 14.04.3 64bit
  • mysql Ver 14.14 Distrib 5.5.47, for debian-linux-gnu (x86_64) using readline 6.3 (installed by just ‘sudo apt-get install …’ command)

My testing results are

i) AppArmor solution only work for /tmp cases.

ii) Following solution works without AppArmor solution.
I would like to appreciate Avnish Mehta for his answer.

$ mysql -u root -p --in-file=1
...
mysql> LOAD DATA LOCAL INFILE '/home/hongsoog/study/mysql/member.dat'
    -> INTO TABLE member_table;

Important three points are

  • start mysql client with –in-file=1 option
  • use LOAD DATA LOCAL INFILE instead of LOAD DATA INFILE
  • check all path element have world read permission from the / to data file path. For example, following subpath should be world readable or mysql group readable if INFILE is targeting for ‘/home/hongsoog/study/mysql/memer.dat’

    • /home
    • /home/hongsoog
    • /home/hongsoog/study/mysql
    • /home/hongsoog/study/mysql/member.data

When you start mysql client WITHOUT “–in-file=1” option and use

LOAD DATA LOCAL INFILE ...

,
you will get

ERROR 1148 (42000): The used command is not allowed with this MySQL version


In summary, “–in-file=1” option in mysql client command and “LOAD DATA LOCAL INFILE …” should go hand in hand.

Hope to helpful to anyone.

Questions:
Answers:

load data infile '/root/source/in.txt' into table employee; ==> Error Code:13

load data infile '/tmp/in.txt' into table employee; ==> works

CentOS release 6.6 (Final) – 5.5.32 MySQL Community Server (GPL)

something related to Linux file permissions

Questions:
Answers:

If you’re using cPanel or phpmyadmin to import the CSV using LOAD DATA then be sure to Enable Use LOCAL keyword. This worked for me in a shared server environment.

Questions:
Answers:

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, 5000 rows affected, 29684 warnings (2.24 sec)