Move MySQL data directory

These are the steps to move the MySQL data directory to another folder:

~# service mysql stop
~# rsync -vaP /var/lib/mysql /new_path/mysql
~# chown -R mysql:mysql /new_path/mysql
~# cp -vp /etc/mysql/my.cnf /etc/mysql/my.cnf.orig
~# vim /etc/mysql/my.cnf

-(del) datadir = /var/lib/mysql
+(add) datadir = /new_path/mysql

~# cp -vp /etc/apparmor.d/usr.sbin.mysqld /etc/apparmor.d/usr.sbin.mysqld.orig
~# vim /etc/apparmor.d/usr.sbin.mysqld

-(del) /var/lib/mysql/ r,
+(add) /new_path/mysql/ r,

-(del) /var/lib/mysql/** rwk,
+(add) /new_path/mysql/** rwk,

~# service apparmor restart
~# mv -v /var/lib/mysql /var/lib/mysql.orig
~# ln -s /new_path/mysql /var/lib/mysql
~# service mysql start

Naturally, “don’t try this at home, kids!” ;)

In order to use Disqus to comment out you have to accept the use of third-party cookies.