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

0 min read 122 words

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
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags

Recent Posts