LOAD DATA INFILE Error Code : 13

后端 未结 19 2359
有刺的猬
有刺的猬 2020-11-28 02:37

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.         


        
相关标签:
19条回答
  • 2020-11-28 03:35

    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

    0 讨论(0)
  • 2020-11-28 03:39

    I too have struggled with this problem over the past few days and I too turned to stack overflow for answers.

    However, no one seems to mention the simplest way to import data into MySQL is actually through their very own import data wizard tool!!

    Just right-click on the table and it comes up there.

    just right click on the table and it comes up there

    None of the above answers helped me, so just use this if you're stuck! :)

    0 讨论(0)
  • 2020-11-28 03:40

    I know, this is an old thread but there are still many folks facing problems with LOAD DATA INFILE!

    There are quite a few answers which are great but for me, none of those worked :)

    I am running MariaDB using mariadb/server docker container, it runs on Ubuntu but I did not have any issues with apparmor

    For me the problem was quit simple, I had the file in /root/data.csv and of course mysql user can't access it!

    On the other hand, folks recommending LOAD DATA LOCAL INFILE as an alternative, while it works, but its performance is not as good as the standard "LOAD DATA INFILE" because when using "LOCAL" it assumes the file is being loaded from a remote terminal and its handling becomes different.

    For best performance, use "LOAD DATA INFILE" always unless you have to load the file from a remote server or your laptop/desktop directly. This is of course disabled due to security reasons so you have to allow "LOCAL_INFILE" through your server's config file "/etc/my.cnf" or "/etc/my.cnf.d/server.cnf" depending on your OS.

    Finally, for me the container had "secure_file_priv" parameter defined to "/data"

    b510bf09bc5c [testdb]> SHOW GLOBAL VARIABLES LIKE '%SECURE_FILE_%';
    +------------------+--------+
    | Variable_name    | Value  |
    +------------------+--------+
    | secure_file_priv | /data/ |
    +------------------+--------+
    

    This means, that the LOAD DATA INFILE will only work if the data file is being loaded from "/data" folder! Watch out for that and use the correct folder :)

    Hope this helps someone.

    Cheers.

    0 讨论(0)
  • 2020-11-28 03:41

    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

    0 讨论(0)
  • 2020-11-28 03:41

    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.

    0 讨论(0)
  • 2020-11-28 03:41

    CentOS 7+ Minimal Secure Solution: (should work with RedHat too)

    chcon -Rv --type=mysqld_db_t /YOUR/PATH/
    

    Explanation:

    Knowing that you applied the good practice of using secure-file-priv=/YOUR/PATH/ in my.cnf in order to use load data infile sql statement, you still see the following:

    ERROR 13 (HY000): Can't get stat of '/YOUR/PATH/FILE.EXT' (Errcode: 13 "Permission denied")

    That's caused by SELinux Enforcing mode, it's not secure to change the mode to Permissive or disable SELinux.
    In short,

    chcon change SELinux security context of a file or files/directories in a similar way to how 'chown' or 'chmod' may be used to change the ownership or standard file permissions of a file.

    SELinux Enforcing mode prevents mysqld from accessing directories with secure-context-type default_t, hence we need to change the secure-context-type of our path to mysqld_db_t. To see the current secure-context-type use the command:

    ls --directory --scontext /YOUR/PATH/
    

    In case you want to reset/undo the solution, then apply below command:

    restorecon -Rv /YOUR/PATH/
    

    The solution I shared is referenced in below links:

    https://wiki.centos.org/HowTos/SELinux
    https://mariadb.com/kb/en/selinux/
    https://linux.die.net/man/8/mysqld_selinux

    0 讨论(0)
提交回复
热议问题