Ubuntu: Move MySQL from /var/lib to /srv

The trick here is to tell apparmor that it is ok for MySQL to access /srv/mysql

First, become sudo user, and stay that way for a bit:

sudo -s

Now, run the following commands

mkdir /srv/mysql
cp -av /var/lib/mysql/* /srv/mysql/
chown -R mysql. /srv/mysql
vi /etc/apparmor.d/usr.sbin.mysqld

Add/edit the lines to say:

/srv/mysql/ r,
/srv/mysql/** rwk,

...instead of the default /var/lib

Next, restart apparmor:

/etc/init.d/apparmor restart

 

Now just edit the /etc/mysql/my.cnf file, and change the datadir to /srv/mysql:

vi /etc/mysql/my.cnf

datadir = /srv/mysql

 

Finally, restart everything and clean up the old /var/lib/mysql:

 

/etc/init.d/mysql restart
rm -rf /var/lib/mysql

All done, just exit out of sudo mode, and enjoy