Step 1:

Login to your MySQL server, enter your password when prompted:

1
mysql -u root -p

Find out where the data directory is located:

1
mysql> select @@datadir;
1
2
3
4
5
6
7
Output
+-----------------+
| @@datadir       |
+-----------------+
| /var/lib/mysql/ |
+-----------------+
1 row in set (0.00 sec)

Step 2:

Now you can stop the server and check the status:

1
sudo systemctl stop mysql
1
sudo systemctl status mysql

It’s time to make a copy to your new mount location:

1
sudo rsync -av /var/lib/mysql /mnt/new-volume-01

Remember to create a backup of the original data in case you need to revert at a later stage:

1
sudo mv /var/lib/mysql /var/lib/mysql.bak

Step 3:

Edit the MySQL configuration to update the datadir to the new location:

1
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf

Find the datadir key and swap the value with your new mount path:

1
2
3
...
datadir=/mnt/new-volume-01/mysql
...

Step 4:

Update AppArmor to point to a new location:

1
sudo vi /etc/apparmor.d/tunables/alias

Create a new line at the bottom that references your new mount path, aliased back to /var/lib/mysql.

1
2
3
4
...
[label /etc/apparmor.d/tunables/alias]
alias /var/lib/mysql/ -> /mnt/new-volume-01/mysql/,
...

Restart AppArmor to pull in the new configuration.

1
sudo systemctl restart apparmor

Step 5:

Start the MySQL server for changes to take effect.

1
2
sudo systemctl start mysql
sudo systemctl status mysql

Login to MySQL again to verify it is using the new directory:

1
mysql -u root -p

Re-run the command from before to verify:

1
mysql> select @@datadir;
1
2
3
4
5
6
7
Output
+---------------------------+
| @@datadir                 |
+---------------------------+
| /mnt/new-volume-01/mysql/ |
+---------------------------+
1 row in set (0.00 sec)