How to Safely Change Location of the MySQL Data Directory (partition/disk)

Most Used Cases are:
- Useful for migration of the MySQL to SSD Disk and to get the best performances.
- Useful if you are out of the disk space on your partition or a disk.

In this tutorial we will move MySQL to the /home folder.

First Make the Backup of all databases

mysqldump --add-drop-table --all-databases | gzip > /home/alldatabases.sql.gz

Lets start migrating MySQL

service mysqld stop
cd /var/lib
mv mysql /home/mysql
ln -s /home/mysql mysql
chown -R mysql:mysql /home/mysql
service mysqld start

for CentOS 7
When using MariaDB and if you are moving it to /home then you also need to edit mariadb service file:
/usr/lib/systemd/system/mariadb.service and change ProtectHome=true to ProtectHome=false

After making changes, restart the service

systemctl daemon-reload
service mysql restart

That is it, no other changes are required.

CloudLinux & CageFS
If you are running CloudLinux then you will need to update CageFS skeleton.
Once you have done migration proceed with:

/usr/sbin/cagefsctl -u