When managing cPanel/WHM servers, system administrators often face disk space shortages on

the /var partition. Since MySQL/MariaDB databases are stored by default in /var/lib/mysql, this

directory fills up quickly. The safest solution is to move the MySQL data directory to a partition with

more space, such as /home. This step-by-step guide ensures a smooth and reliable migration

without risking data loss.

In this article, we’ll walk you step by step through the process of changing the MySQL/MariaDB data directory on cPanel/WHM servers safely, without risking data loss.

Why Change the MySQL/MariaDB Data Directory?

  • Partition Space Limitations – /var fills up quickly, while /home often has more space.
  • Performance Optimization – Better I/O performance on faster partitions. Custom Configurations– compliance with storage and organizational policies.

Step-by-Step Guide to Move MySQL/MariaDB Data Directory

  1. Backup All Databases

Always take a full backup to avoid data loss in case of errors:

tar -cvf mysql.tar /var/lib/mysql

  1. Stop the MariaDB Service

On SystemD:

systemctl stop mariadb

systemctl is-active mariadb

On SysVInit:

service mysqld stop

service mysqld status

 

  1. Install and Use Screen (Optional)

Screen ensures the process continues even if your session disconnects:

yum install screen

Screen

  1. Sync Databases to /home/mysql

Use rsync to copy files safely with permissions preserved:

rsync -avz /var/lib/mysql /home

This creates /home/mysql and copies all data there.

 

  1. Resume/Manage Screens (Optional)

You can manage your screen sessions with:

screen -r (resume)

Ctrl + A + D (detach)

Ctrl + A + ? (help menu)

  1. Edit MySQL Configuration

Open and update configuration files:

vi /etc/my.cnf

Change or add:

datadir=/home/mysql

Also verify /etc/my.cnf.d/*.cnf for duplicate datadir entries.

  1. Relink the Socket File

MySQL requires its socket file. Recreate the symlink:

rm -rf /tmp/mysql.sock

ln -sf /home/mysql/mysql.sock /tmp/mysql.sock

Note: On some cPanel servers, the socket may be in /var/lib/mysql/mysql.sock. Adjust configs if

Necessary.

  1. Update MariaDB Service File

To ensure MariaDB can run from /home, edit systemd service:

vi /usr/lib/systemd/system/mariadb.service

Change:

ProtectHome=true ® ProtectHome=false

  1. Start MariaDB Service

Reload systemd and restart MariaDB:

systemctl daemon-reload

systemctl start mariadb

If you see warnings, reboot and restart the service.

  1. Verify the Migration

From WHM/cPanel ® MySQL Database Wizard, create a test database.

If the new database appears in /home/mysql, migration is successful.

  1. Clean Up Old Data Directory

Do not delete immediately. Instead, rename first:

mv /var/lib/mysql /var/lib/mysql.bak

After verifying stability for 1–2 days:

rm -rf /var/lib/mysql.bak

Always back up before making changes. Use rsync instead of mv to avoid corruption. Check both /etc/my.cnf and /etc/my.cnf.d/ configs for datadir. Keep old directory as backup before deleting permanently. Test thoroughly before deploying to production.

At ServerAdminz, our Linux experts perform such optimizations daily to ensure server uptime, security, and performance remain unaffected. If you need help with MySQL migrations, server hardening, or 24/7 server management, check out our detailed guide on MySQL Database Migration via Command Line.

For more MySQL tutorials and best practices, explore our complete blogs here: MySQL articles from ServerAdminz