Wednesday, August 27, 2014

Ubuntu - How to Relocate MySQL Data Directory

For anyone that is interested in moving the MySQL default datadir, here are the steps:

1. Stop the mySQL service:
# service mysql stop

2. Create the new data directory:
# mkdir /data2/mysql

3. Sync the current datadir with the new one:
# rsync -av /var/lib/mysql /data2/mysql

4. Update my.cnf file
# vi /etc/mysql/my.cnf
# update datadir in [mysqld]
datadir         = /data2/mysql

5. MySQL use AppArmor to manage its permissions, you need to update AppArmor settings for MySQL as well:
# vi /etc/apparmor.d/usr.sbin.mysqld
# Comment out
/var/lib/mysql/ r,
/var/lib/mysql/** rwk,
# Add new
/data2/mysql/ r,
/data2/mysql/** rwk,

6. Start MySQL:
# service mysql start

Your MySQL datadir should be updated now.

No comments: