How to Change a MariaDB/MySQL Data Directory to a New Location on Linux

  • Home /
  • Blog Posts /
  • How to Change a MariaDB/MySQL Data Directory to a New Location on Linux

Step 1 — Moving the MariaDB Data Directory

mysql -u root -p
select @@datadir;
Output:
  +-----------------+
  | @@datadir       |
  +-----------------+
  | /var/lib/mysql/ |
  +-----------------+
  1 row in set (0.00 sec)
exit
sudo systemctl stop mariadb
sudo systemctl status mariadb
Output:
  mysql systemd[1]: Stopped MariaDB database server.
sudo rsync -av /var/lib/mysql /mnt/my-volume-01
sudo mv /var/lib/mysql /var/lib/mysql.bak

Step 2 — Pointing to the New Data Location

sudo vi /etc/my.cnf
[mysqld]
. . .
datadir=/mnt/my-volume-01/mysql
socket=/mnt/my-volume-01/mysql/mysql.sock
. . .

[client]
port=3306
socket=/mnt/my-volume-01/mysql/mysql.sock

!includedir /etc/my.cnf.d

Step 3 — Restarting MariaDB

sudo systemctl start mariadb
sudo systemctl status mariadb
mysql -u root -p
select @@datadir;
Output
+----------------------------+
| @@datadir                  |
+----------------------------+
| /mnt/my-volume-01/mysql/ |
+----------------------------+
1 row in set (0.01 sec)
exit

Cleanup

sudo rm -Rf /var/lib/mysql.bak