Last updated on October 4th, 2020 at 01:30 pm
In a Linux bases system, by default MySQL/MariaDB data directory will be /var/lib/mysql.
As a good practice it is always recommended to change MySQL / MariaDB Default data directory. For example in a production network where large scale applications expected to grow due to high usage. In this case it is not recommended to use / partition to store MySQL data files. Otherwise, the filesystem where /var/lib/mysql is located may collapse at one point causing the entire system to fail.
Another scenario where changing the default directory is when we have a dedicated storage network that we want to store our critical data. So to be on safe side, In this guide we will explain how to Change MySQL / MariaDB Default Data Directory to a different location and also we will see how we can safely move old databases.
For this guide I am using CentOS 8 Operating system with MariaDB database server.
Instructions to Change MySQL / MariaDB Default Data Directory
In this guide I am going to use /home/db-data directory as my new data directory for MariaDB.
1. Stop Mysql/MariaDB
Before making any changes, first make sure to stop MariaDB service using the following command.
2. Move Old Data (Old Databases and MariaDB Files).
Copy entire mysql director from old directory /var/lib/mysql to our new directory /home/db-data using below command.
3. Edit MySQL Config File
As a good practice backup MariaDB configuration files before making any changes.
Server config file
Backup of client config file
Now edit /etc/my.cnf.d/mariadb-server.cnf file then look for the entry for datadir and socket, then change the current path which is /var/lib/mysql to the new data directory which is /home/db-data/.
Now edit another configuration file /etc/my.cnf.d/client.cnf and update path to new location as showing below.
4. Finally start MySQL service
Now you can login to MySQL and you will see the same databases you had before.