MySQL won't start after moving data to new location

Recently I had to move the MySQL data from the standard location (/var/lib/mysql) to a new location because my root filesystem was full.
This was an older Ubuntu 14.04 server with (unfortunately) no LVS configured.

So I decided to move my MySQL data to a seperate partition which I used for /home.

This is what happend:

Stop MySQL

service mysql stop

Copy the data to the new location:

cd /var/lib/mysql/
cp -rp * /home/mysql/

Change the datadir variable in the my.cnf:

datadir         =/var/lib/mysql


datadir         = /home/mysql


When I wanted to start MySQL again I got these error messages in my /var/log/mysql/error.log logs:

150819 14:52:49 [Note] /usr/sbin/mysqld (mysqld 5.5.43-0ubuntu0.14.04.1) starting as process 30201 ...
150819 14:52:49 [Warning] Can't create test file /home/mysql/storage.lower-test
150819 14:52:49 [Warning] Can't create test file /home/mysql/storage.lower-test
150819 14:52:49 [Warning] Using unique option prefix myisam-recover instead of myisam-recover-options is deprecated and will be removed in a future release. Please use the full name instead.
150819 14:52:49 [Note] Plugin 'FEDERATED' is disabled.
/usr/sbin/mysqld: Can't find file: './mysql/plugin.frm' (errno: 13)
150819 14:52:49 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
150819 14:52:49 InnoDB: The InnoDB memory heap is disabled
150819 14:52:49 InnoDB: Mutexes and rw_locks use GCC atomic builtins
150819 14:52:49 InnoDB: Compressed tables use zlib 1.2.8
150819 14:52:49 InnoDB: Using Linux native AIO
150819 14:52:49 InnoDB: Initializing buffer pool, size = 128.0M
150819 14:52:49 InnoDB: Completed initialization of buffer pool
150819 14:52:49  InnoDB: Operating system error number 13 in a file operation.
InnoDB: The error means mysqld does not have the access rights to
InnoDB: the directory.
InnoDB: File name ./ibdata1
InnoDB: File operation call: 'open'.
InnoDB: Cannot continue operation.
150819 14:52:49 mysqld_safe mysqld from pid file /var/run/mysqld/ ended


So MySQL didn't start because several files couldn't be read or written.

My dmesg was telling me that the Apparmor process was preventing MySQL to read the necessary files:


[2562909.289250] type=1400 audit(1439987402.901:104): apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/home/mysql/ibdata1" pid=29238 comm="mysqld" requested_mask="rw" denied_mask="rw" fsuid=117 ouid=117


So now we need to change the rights so Apparmor can allow MySQL to read the files from the new location:

open /etc/apparmor.d/usr.sbin.mysqld

Change the lines

  /var/lib/mysql/ r,
  /var/lib/mysql/** rwk,


  /home/mysql/ r,
  /home/mysql/** rwk,

Restart Apparmor

service restart apparmor

Start MySQL

service mysql start

And now it should work again.