How to change MySQL Datadir in Cpanel Server

If the disk partition that stores MySQL data directory runs out of disk space, you may need to consider moving the MySQL data directory to another partition. Before you do the MySQL data directory change, make sure you take a backup of all your databases using mysqldump.

First, disable MySQL monitoring, so MySQL won’t auto start while we do the data directory migration

Stop the MySQL server

In this case, we have free disk space in /home partition so we will move the MySQL data directory to this partition. Create a directory to store MySQL data, and copy the MySQL files to it.

Rename /var/lib/mysql directory, create an empty directory for MySQL socket

Update my.cnf file

Add

MariaDB 10.1 or newer does not allow MySQL data in /home directory. To make datadir work from /home, edit file

Add

Since we edit systemd file, we need to reload

Start MySQL

At this point, MySQL will work from the new data directory. Verify sites are working properly.

If all is good, enable cpanel service monitoring for MySQL

If you are using CloudLinux CageFS, sites won’t work. You need to run these 2 commands to get MySQL working for websites.

Remove old MySQL datadir

We have renamed original MySQL data directory as /var/lib/mysql-backup, you can take a backup of this directory and delete it to free up disk space.

Back to MySQL

Need help with Linux Server or WordPress? We can help!

Leave a Reply

Your email address will not be published. Required fields are marked *