Learn how to Move a MySQL Data Directory to a New Location on Ubuntu 20.04 safely with clear steps and commands for a smooth configuration. Our MySQL Live Support Team is always here to help you.

Move a MySQL Data Directory to a New Location on Ubuntu 20.04

When your MySQL data starts to grow, your default storage may not be enough. Shifting the data to a larger or faster drive is a smart move, but it must be done carefully. In this guide, we’ll see how to Move a MySQL Data Directory to a New Location on Ubuntu 20.04 without breaking anything. Every step below matters, so follow along closely.

Move a MySQL Data Directory to a New Location

Finding and Preparing the Current MySQL Data Directory

Before moving anything, let’s confirm where MySQL currently stores its data. Start an interactive MySQL session using:

sudo mysql

If your MySQL root user uses password authentication, connect like this instead:

mysql -u root -p

Once you’re in, check the current data directory:

SELECT @@datadir;

You’ll see it’s using /var/lib/mysql/, the default path. Type exit to leave MySQL.

Now, to keep everything safe, stop the MySQL service:

sudo systemctl stop mysql

If you want to double-check, run:

sudo systemctl status mysql

You should see Status: “Server shutdown complete”, confirming it’s inactive.

Next, copy your current database directory to the new location. Here’s the command:

sudo rsync -av /var/lib/mysql /mnt/volume-nyc1-01

Avoid adding a trailing slash after /var/lib/mysql, or rsync will misplace your files.

Once done, rename the original folder as a backup:

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

Now you’re ready for configuration.

Updating MySQL Configuration for the New Directory

Open the MySQL configuration file:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Look for the line that starts with datadir=. Uncomment it and replace the path with the new location:

datadir=/mnt/volume-nyc1-01/mysql

Save and exit the editor by pressing CTRL + X, then Y, and ENTER.

Adjusting AppArmor Permissions

AppArmor controls which paths MySQL can access. So, you’ll need to update its rules. Open the alias configuration:

sudo nano /etc/apparmor.d/tunables/alias

At the bottom, add this line:

alias /var/lib/mysql/ -> /mnt/volume-nyc1-01/mysql/,

Then restart AppArmor to apply changes:

sudo systemctl restart apparmor

If you skip this, MySQL won’t start and will show:

Job for mysql.service failed because the control process exited with error code.

Now you know what causes that message, AppArmor not allowing access.

Move Your MySQL Data Today!

Chat animation


Getting MySQL Running Again

There’s one last hurdle before restarting MySQL. The script /usr/share/mysql/mysql-systemd-start checks for the default directory before allowing startup. Inspect it:

nano /usr/share/mysql/mysql-systemd-start

You’ll find these lines:

if [ ! -d /var/lib/mysql ] && [ ! -L /var/lib/mysql ]; then
echo "MySQL data dir not found at /var/lib/mysql. Please create one."
exit 1
fi
if [ ! -d /var/lib/mysql/mysql ] && [ ! -L /var/lib/mysql/mysql ]; then
echo "MySQL system database not found. Please run mysql_install_db tool."
exit 1
fi

Close the file without changing anything.

Now, create a dummy directory to satisfy the check:

sudo mkdir /var/lib/mysql/mysql -p

Then bring MySQL back up:

sudo systemctl start mysql

Check its status:

sudo systemctl status mysql

Confirming the New MySQL Data Location

Let’s make sure the directory change actually worked. Log in again:

mysql -u sammy -p

And verify the data directory:

SELECT @@datadir;

If it shows your new path, perfect. Type exit to leave.

Finally, remove your backup to free up space:

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

Restart MySQL once more for good measure:

sudo systemctl restart mysql

Then confirm everything is running:

sudo systemctl status mysql

If the Active line reads active (running), your setup is successful.

Conclusion

That’s how you Move a MySQL Data Directory to a New Location on Ubuntu 20.04, safely, efficiently, and with every detail intact. It’s a simple process once you understand why each command matters. So, the next time your storage needs change, you know exactly how to move your MySQL data without a single hiccup.