Fixing MySQL error 29 (ErrCode: 13) in Ubuntu

Ubuntu 12.04 Recently I had to work with a large amount of data that had to be imported into MySQL via a script after being modified. I’ve found that using LOAD DATA was the quickest way to do the job.

If you are using Ubuntu for your server (or you have to work with a server using Ubuntu) you will find this error when trying to use the query:

ERROR 29 (HY000): File '....' not found (Errcode: 13)

After few seconds on Google you will find many users that managed to get over the error using the keyword LOCAL, but if you keep your MySQL updated you will see that it will not work as it could generate a security issue in your server.

To overcome this issue there is a simple workaround that require you to modify the MySQL’s AppArmor file and define directory where the program can read and write.

Be aware that allowing MySQL to read and write in a directory could lead to security problems, always review this kind of changes with a System Administrator.

The workaround is really quick:

  1. Open the file /etc/apparmor.d/usr.sbin.mysqld as root with your favourite text editor
  2. You should see a list of paths where AppArmor allow MySQL to write and read, at the end of this list add the directory where you would like that MySQL will be able to read and write following the pattern used in all the previous entries. For instance:
    #Other contents
    /usr/sbin/mysqld {
        #Other contents
        /var/log/mysql.log rw,
        /var/log/mysql.err rw,
        #Other contents
        #This will be your dir definition
        /tmp/ r,
        /tmp/* rw,
        #Other contents

    Then save and close the file.

  3. After you have successfully modified the file it is time to tell AppArmor that it needs to reload the configurations, so as root execute the following command:
    # /etc/init.d/apparmor reload
  4. Then restart MySQL, as root execute the following command:
    # service mysql restart

At this point you have allowed MySQL to write and read contents into your defined directory, only do this if you know what you are doing! I take no responsibility for any damage or data loss caused by a server not correctly secured.

Be careful & have fun!