
Fabian Tech Tips

Changing the data directory for MariaDB might seem like a complex task, but with a clear, step-by-step guide, it becomes manageable
Oct 13, 2024
2 min read
0
2
0
Changing the data directory for MariaDB might seem like a complex task, but with a clear, step-by-step guide, it becomes manageable. In this blog post, we’ll walk you through the detailed process of moving your MariaDB data directory from /var/lib/mysql/ to a new location at /mnt/disk00/mysql.
By following these steps, you’ll ensure that all your database files are properly migrated and secured in the new directory, minimizing downtime and maintaining database integrity. Whether you’re doing this for performance optimization, storage management, or other reasons, this guide will provide you with the knowledge and confidence to execute the change smoothly.
Read on for a detailed explanation, including how to stop the MariaDB service, set permissions, update configurations, and verify the changes. Let's dive in!
Detailed Explanation for Changing MariaDB Data Directory
To change the data directory for MariaDB from /var/lib/mysql/ to /mnt/disk00/mysql, follow these steps:
Stop MariaDB Service:
sudo systemctl stop mariadb
Create the New Data Directory:
sudo mkdir -p /mnt/disk00/mysql
Set Permissions:
sudo chown -R mysql:mysql /mnt/disk00/mysql
Copy Existing Data:
sudo rsync -av /var/lib/mysql/ /mnt/disk00/mysql/
Update MariaDB Configuration:
Open the MariaDB configuration file, typically located at /etc/my.cnf or /etc/mysql/my.cnf.
Find the [mysqld] section and update the datadir parameter:
[mysqld]
datadir=/mnt/disk00/mysql
Update AppArmor or SELinux Configuration (if applicable):
For AppArmor, update the profile in /etc/apparmor.d/usr.sbin.mysqld:
/mnt/disk00/mysql/ r,
/mnt/disk00/mysql/** rwk,
Reload AppArmor:
sudo systemctl reload apparmor
Verify Permissions:
sudo chown -R mysql:mysql /mnt/disk00/mysql
sudo chmod 755 /mnt/disk00/mysql
Start MariaDB Service:
sudo systemctl start mariadb
Verify the New Data Directory:
Check the status of MariaDB to ensure it is running correctly:
sudo systemctl status mariadb
Verify that the new data directory is being used:
mysql -u root -p -e "SHOW VARIABLES LIKE 'datadir';"
These steps will change the data directory for MariaDB to /mnt/disk00/mysql, ensuring that all database files are stored in the new location.