How To Change a MariaDB Data Directory to a New Location on CentOS
How To Change a MariaDB Data Directory to a New Location on CentOS
1 — Moving the MariaDB Data Directory
To prepare for moving MariaDB’s data directory, let’s verify the current location by starting an interactive session using the administrative credentials.
#mysql -u root -p
When prompted, supply the MariaDB root password. Then from the mysql
prompt, select the data directory:
> select @@datadir;
/var/lib/mysql/
This output confirms that MariaDB is configured to use the default data directory, /var/lib/mysql/,
so that’s the directory we need to move.
To ensure the integrity of the data, we’ll stop service - MariaDB before we actually make changes to the data directory:
# systemctl stop mariadb
to check if the service is stopped use the command:
#systemctl status mariadb
Sep 11 12:21:04 mysql systemd[1]: Stopped MariaDB database server.
Now that the server is shut down, we’ll copy the existing database directory to the new location with rsync
. Using the -a
flag preserves the permissions and other directory properties, while-v
provides verbose output so you can follow the progress.
Note: Be sure there is no trailing slash on the directory, which may be added if you use tab completion. When there’s a trailing slash, rsync
will dump the contents of the directory into the mount point instead of transferring it into a containing mysql
directory:
#rsync -av /var/lib/mysql /mnt/volume-mariadb
Once the rsync
is complete, rename the current folder with a .bak extension and keep it until we’ve confirmed the move was successful. By re-naming it, we’ll avoid confusion that could arise from files in both the new and the old location:
#mv /var/lib/mysql /var/lib/mysql.bak
Step 2 — Pointing to the New Data Location
MySQL has several ways to override configuration values. By default, the datadir
is set to /var/lib/mysql
in the /etc/my.cnf
file. Edit this file to reflect the new data directory:
#vi /etc/my.cnf