LINUX – Move MySQL data directory

LinuxIn newer versions of Linux it’s not easily possible to just move the MySQL data directory to another location and modify my.cnf or create a symbolic link. There is a secutity/protection “daemon” running in background checking which locations is some process trying to access. If it’s allowed, then access is granted, otherwise the process is unable to read/write the required data. This guardian is called “APPARMOR”

I wanted to move the MySQL data directory (among others, like /var/www, git projects repositories, owncloud data directory, media like photos, videos, music, …) to another location on my file system due to separation of data from the rest of the running system – in case of failure I’d just re-install the system, but data stored on separated partition will remain untouched and can just be binded/mounted to the fresh installed system.

Let’s assume I’ve my data partition mounted as /mnt/data. So I moved the MySQL data directory there using:

sudo mv /var/lib/mysql /mnt/data

And created a symbolic link on original location

cd /var/lib
sudo ln -s /mnt/data/mysql

When I tried to start MySQL server daemon I got the following error message:

start: Job failed to start

So I edited the Apparmor configuration alias file for MySQL:

sudo nano /etc/apparmor.d/tunables/alias

with the following content:

...
alias /var/lib/mysql/ -> /backup/mysql/,
...

and reloaded the configuration using command

sudo /etc/init.d/apparmor reload

Now the MySQL server should be started correctly

mysql start/running, process 27288

Leave a Reply