AppArmor Locking MySql moved data folder in Ubuntu

I found that AppArmor was the culprit by examining the syslog, and was able to successfully change the mysql data location by following this process.

 

Please note that, in files edited below, lines starting with + were added, and lines starting with – were removed. You should not actually type/paste the + signs when adding lines to these files.

 

I cloned the mysql directory to the new location:

 

sudo rsync -av /var/lib/mysql /new_dir

Then I edited the datadir line in /etc/mysql/my.cnf:

 

sudo vi /etc/mysql/my.cnf

-datadir = /var/lib/mysql

+datadir = /new_dir/mysql

Then I edited /etc/apparmor.d/usr.sbin.mysqld:

 

sudo vi /etc/apparmor.d/usr.sbin.mysqld

– /var/lib/mysql/ r,

– /var/lib/mysql/** rwk,

+ /new_dir/mysql/ r,

+ /new_dir/mysql/** rwk,

Then I restarted mysql.

 

More info here

https://help.ubuntu.com/community/AppArmor