How to Fix a Can't Create/Write to File Error With SELECT INTO OUTFILE in MySQL
Have you ever tried to use SELECT…INTO OUTFILE
in MySQL and come across the following error?
"Can't create/write to file '/path/to/folder/filename' (Errcode: 2)"
I was having this problem the other day and, after checking that file permissions weren’t the cause of the problem, I came across the following post on the MySQL forums:
"The problem was not in MySQL but in AppArmor on Ubuntu. I had to add the directories I wanted to write into to my /etc/apparmor.d/usr.sbin.mysqld and restart apparmor.d."
So, one sudo vim /etc/apparmor.d/usr.sbin.mysqld
later and I’d added the path/to/folder
I needed to be able to write to (not forgetting to add the trailing comma ‘,’ to the end of the line, which is always required, even for the last line).
All that was left to do was to restart AppArmor with:
$ sudo /etc/init.d/apparmor restart
After that, SELECT…INTO OUTFILE
worked like a charm :)