moving mysql data directory
In some cases you have to change the default MySql data directory, for doing this you have to follow some steps. Here I will explain each steps one by one.
- Stop MySql server using following command.
- Copy the existing data directory (which is located in /var/lib/mysql) using following command.
- Edit the MySQL configuration file with your favorite text editor.
- Find the entry for datadir, change the path to the new data directory.
- Some Linux distributions uses some security software called AppArmor that specifies the areas of your filesystem applications are allowed to access. Unless you modify the AppArmor profile for MySQL, you’ll never be able to restart MySQL with the new datadir location. So edit the file /etc/apparmor.d/usr.sbin.mysqld using your favorite editor.
- Duplicate the lines beginning with /var/lib/mysql and replace duplicated strings with /new_datadir.
- Restart the AppArmor profiles with the following command.
- Restart the MySql server with the following command.
$ sudo /etc/init.d/mysql stop
$ sudo cp -R -p /var/lib/mysql /path/to/new/datadir
$ sudo vim /etc/mysql/my.cnf
$ sudo vim /etc/apparmor.d/usr.sbin.mysqld
/var/lib/mysql/ r, /var/lib/mysql/** rwk, /var/log/mysql/ r, /var/log/mysql/* rw, # /path/to/new/datadir r, /path/to/new/datadir** rwk, /path/to/new/datadir r, /path/to/new/datadir* rw,
$ sudo /etc/init.d/apparmor restart
$ sudo /etc/init.d/mysql start
MySQL should now start without any errors and your data will be stored in the new location.
Posted by Shahid