πŸ“¦ Moving MariaDB Data Directory on AlmaLinux 9 / CloudLinux 9 Servers (LiteSpeed + cPanel)

πŸ“ 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 like ProtectHome.
  • 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.

πŸ›‘οΈ 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.

Scroll to Top