01.08.2023

How to change the MariaDB data directory to a new location in CentOS 7

What is MariaDB

MariaDB is very popular SQL-based dadabase system. It is more powerful and fully compatible with it's "ancestor" - MySQL.

Why might you need to move MariaDB data directory to another location

There are two mainly reasons:

How to change database data location

Before any opeations I strongly advice you to ensure what is current data location. Just open an SSH-session as root user, login to the MariaDB and run this command:

mysql -u root

select @@datadir;

Quit and stop the service:

quit;

service mariadb stop

Create new directory and move databases data into:

mkdir <path_to_directory> && rsync -av <current_data_directory> <new_data_directory>

Edit database service configuration file:

sed -i 's|<current_data_directory>|<new_data_directory>|g' /etc/my.cnf

cat <<EOT >> /etc/my.cnf
[client]
socket=<new_data_directory>/mysql.sock
EOT

Create special socket-file and set permissions:

touch <new_data_directory>/mysql.sock && chmod 777 <new_data_directory>/mysql.sock

Start the service:

service mariadb start

 

Check where is data located now:

mysql -u root

select @@datadir;

Conclusion

After this article reading you knew what is MariaDB, possible reasons to move its data to other location and how to do this.