How to Move MySQL’s Data Directory on Ubuntu
Oct 12
I recently setup a MySQL database on EC2 and wanted to move the data directory off the root partition. For the sake of simplicity I wanted MySQL’s data files to live in /mnt/mysql. I thought this would be a trivial task and after moving the files and updating /etc/mysql/my.cnf data to:
datadir = /mnt/mysql
I restarted MySQL and noticed the following error in the log file:
/usr/sbin/mysqld: Can’t find file: ‘./mysql/plugin.frm’ (errno: 13)
111012 1:10:01 [ERROR] Can’t open the mysql.plugin table. Please run mysql_upgrade to create it.
111012 1:10:01 InnoDB: Initializing buffer pool, size = 8.0M
111012 1:10:01 InnoDB: Completed initialization of buffer pool
111012 1:10:01 InnoDB: Operating system error number 13 in a file operation.
MySQL wouldn’t start so I scratched my head for a bit, did some Googling, and discovered AppArmor on Ubuntu. It turns out that AppArmor restricts the capabilities of any program that has a profile configured. MySQL falls into this category.
So I updated the AppArmor profile for MySQL. This file is located in:
/etc/apparmor.d/usr.sbin.mysqld
You’ll see a couple lines that look like:
/var/lib/mysql/ r,
/var/lib/mysql/** rwk
I changed those to:
/mnt/mysql/ r,
/mnt/mysql/** rwk
and restarted AppArmor via:
sudo /etc/init.d/apparmor restart
Then I started MySQL and was good to go.
Read More




