Centos 7 and MariaDB/MySQL symbolic links or how to set different locations for databases?

This post outlines how to configure MariaDB or MySQL on Centos 7 in order to store a specific database outside of the typical /var/lib/mysql/DBNAME directory. Or in other words, how...

This post outlines how to configure MariaDB or MySQL on Centos 7 in order to store a specific database outside of the typical /var/lib/mysql/DBNAME directory. Or in other words, how you can have each of your databases residing on a different drive (or a different folder on the same drive), while running only a single instance of MariaDB or MySQL.

First of all, open your MariaDB configuration file at /etc/my.cnf file and make sure that symlinks are turn on, it should look like this under [mysqld]:

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=1

Now, stop MySQLD service and move existing database from /var/lib/mysql/DatabaseName to a new location, that can be a drive or folder. Make sure you're moving the directory of the database. Double check that directory /var/lib/mysql/DatabaseName no longer exist in the file system.

As a next step, we'll make a symbolic link, that will link /var/lib/mysql/DatabaseName to new location of the directory (wherever you've copied it in the previous step).

This can be done by running following command:

ln -s /path/to/new/database/directory /var/lib/mysql/DATABASENAME

Now when all is done, start your MariaDB or MySQL and you should see your database even that it resides on a different drive (or is located in a completely different folder).

--

Troubleshooting:

If you run into problems, experiencing errors when trying to access your database, such as “Can't find file: … (errno: 13)”, it'll most likely due to selinux blocking MySQL/MariaDB. Run following line to fix the issue:

setenforce Permissive