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
- Backup All Databases
Always take a full backup to avoid data loss in case of errors:
tar -cvf mysql.tar /var/lib/mysql
- Stop the MariaDB Service
On SystemD:
systemctl stop mariadb
systemctl is-active mariadb
On SysVInit:
service mysqld stop
service mysqld status
- Install and Use Screen (Optional)
Screen ensures the process continues even if your session disconnects:
yum install screen
Screen
- 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.
- Resume/Manage Screens (Optional)
You can manage your screen sessions with:
screen -r (resume)
Ctrl + A + D (detach)
Ctrl + A + ? (help menu)
- 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.
- 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.
- 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
- Start MariaDB Service
Reload systemd and restart MariaDB:
systemctl daemon-reload
systemctl start mariadb
If you see warnings, reboot and restart the service.
- Verify the Migration
From WHM/cPanel ® MySQL Database Wizard, create a test database.
If the new database appears in /home/mysql, migration is successful.
- 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