Moving MySQL Database Folder

By default, MySQL database is stored on ubuntu on “/var/lib/mysql/” folder according to my.cnf. If you want to move the stored folder to another(e.g., /nsm/mysql/), you can refer the script below.

$ service mysql stop
$ cp -avrp /var/lib/mysql /nsm/
$ chown -R mysql:mysql /nsm/mysql/
$ vi /etc/mysql/my.cnf
– datadir=/var/lib/mysql
+ datadir=/nsm/mysql
$ service mysql start

If there is anything for restricting the MySQL database file or folder, you need to re-configure it such as SELinux or AppArmor(“Application Armor”).

$ vi /etc/apparmor.d/usr.sbin.mysqld
– /var/lib/mysql/ r,
– /var/lib/mysql/** rwk,
+ /nsm/mysql/ r,
+ /nsm/mysql/** rwk,
$ /etc/init.d/apparmor restart

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.