Troubleshooting MySQL's SELECT INTO OUTFILE Errcode 13: Beyond Basic Permissions
Using SELECT INTO OUTFILE
to export MySQL data to a CSV can be thwarted by Errcode 13, a permissions error. While insufficient file permissions are the usual culprit, the problem persists even with correct directory permissions, often due to AppArmor's security measures in modern Ubuntu Server versions.
AppArmor's Role in MySQL Permissions
AppArmor, a Linux security module, restricts application access to system resources. If MySQL runs under an AppArmor profile (check with sudo aa-status
; look for "/usr/sbin/mysqld" under "profiles in enforce mode"), its write access might be limited.
Resolving the Issue: Granting AppArmor Access
To allow MySQL to write to specific locations:
Edit the AppArmor profile: Open /etc/apparmor.d/usr.sbin.mysqld
for editing.
Add write permissions: Add lines granting read/write access to the target directory. For instance:
<code>/usr/sbin/mysqld { ... /data/ r, /data/* rw, }</code>
This allows MySQL to read from and write to /data
and its subdirectories.
Reload AppArmor: Execute sudo /etc/init.d/apparmor reload
to apply the changes.
Restart MySQL: Restart the MySQL service. Retry your SELECT INTO OUTFILE
command.
Important Security Note: Carefully consider the implications of granting write access to MySQL via AppArmor. Only grant access to absolutely necessary directories to minimize security risks.
The above is the detailed content of Why Does MySQL SELECT INTO OUTFILE Fail with Errcode 13 Despite Correct File Permissions?. For more information, please follow other related articles on the PHP Chinese website!