π Introduction
On servers running cPanel + LiteSpeed + CloudLinux 9 / AlmaLinux 9, disk space issues on /
(root) can impact MariaDB performance and stability. However, unlike MySQL, MariaDB and systemd impose stricter limitations, especially on moving the data directory.
This guide provides a safe and supported method for moving the MariaDB data directory to a new mount (e.g. a dedicated logical volume), but not to /home
, which is not allowed under systemd
protection policies.
β οΈ Important Considerations
- MariaDB and systemd: Since MariaDB 10.1.16+, systemd disallows data directories under
/home
,/root
,/usr
,/boot
, or/etc
due to security restrictions likeProtectHome
. - Use a Dedicated Mount Point: Use a separate mount (e.g.
/mysql_data
or/data/mysql
) on a different partition or disk. - cPanel + LiteSpeed Compatible: This guide is designed for MariaDB on cPanel servers using LiteSpeed.
- Downtime Required: Ensure maintenance time is scheduled.
π§ Procedure
1οΈβ£ Create Full Backup
Create a full database backup:
mysqldump -A --opt > /root/mariadb_backup.sql
You can later compress the backup if needed.
2οΈβ£ Disable MySQL Monitoring in WHM
whmapi1 configureservice service=mysql enabled=1 monitored=0
3οΈβ£ Stop MariaDB
systemctl stop mariadb
4οΈβ£ Create New Data Directory Mount
β οΈ Do NOT use
/home
If you have a second disk or partition (e.g. /dev/sdb1
), mount it at /mysql_data
:
mkdir /mysql_data
mount /dev/sdb1 /mysql_data
Ensure it’s added to /etc/fstab
for persistence.
Now prepare the MariaDB directory:
mkdir -p /mysql_data/mysql
rsync -av /var/lib/mysql/ /mysql_data/mysql/
Use
rsync -av
to preserve permissions and ownership.
5οΈβ£ Update Permissions
chown -R mysql:mysql /mysql_data/mysql
6οΈβ£ Modify MariaDB Configuration
Edit /etc/my.cnf
and update or add:
[mysqld]
datadir=/mysql_data/mysql
socket=/var/lib/mysql/mysql.sock
β οΈ Donβt move the socket unless you update all dependent services (PHP, LSAPI, etc.).
7οΈβ£ Recreate Socket Directory (if needed)
MariaDB expects the socket directory to exist:
mkdir -p /var/lib/mysql
chown mysql:mysql /var/lib/mysql
8οΈβ£ Update systemd Service File
Create or edit systemd override:
mkdir -p /etc/systemd/system/mariadb.service.d/
nano /etc/systemd/system/mariadb.service.d/override.conf
Add:
[Service]
ProtectSystem=off
ProtectHome=false
Then reload systemd:
systemctl daemon-reexec
systemctl daemon-reload
9οΈβ£ Start MariaDB
systemctl start mariadb
Check logs:
journalctl -xeu mariadb
π Re-enable MySQL Monitoring in WHM
whmapi1 configureservice service=mysql enabled=1 monitored=1
β Verification
Run:
mysql -e "SHOW VARIABLES LIKE 'datadir';"
It should now point to /mysql_data/mysql
.
Also confirm:
lsof -p $(pidof mysqld) | grep /mysql_data
π LiteSpeed + PHP Considerations
- If youβre using LiteSpeed with CloudLinux PHP Selector, confirm that MySQL socket connections are still resolving. If not:
- Check that
/var/lib/mysql/mysql.sock
exists. - If moved, update PHP
.ini
files or/etc/cl.selector/php.conf
.
- Check that
π‘οΈ CloudLinux-Specific Notes
If using CageFS, run:
cagefsctl --force-update
If errors persist (e.g., “500 Internal Server Error” from PHP apps), try:
cagefsctl --rebuild-alt-php-ini
Also confirm mysql.sock
is not blocked by jailed environment or symlink issues.
π fstab Example
If using /dev/sdb1
as the new MySQL mount:
/dev/sdb1 /mysql_data xfs defaults 0 0
Use blkid
to find UUID if preferred.
π Conclusion
You’ve now safely relocated the MariaDB data directory on a modern AlmaLinux 9 / CloudLinux 9 system using cPanel and LiteSpeedβwithout violating systemd protections.
β Always keep backups and verify every service that relies on MySQL is functioning post-move.