Move MySQL's data directory on Ubuntu


MySQL stores its databases in a directory on your server. On Ubuntu, this is usually /var/lib/mysql. If you want to store the files on another disk, you can copy them to the new location and tell MySQL where to find them.

This guide assumes MySQL is already installed. The commands use /mnt/data/mysql as an example destination only. Replace it with the path where your disk is mounted, and use your chosen path everywhere /mnt/data/mysql appears below. MySQL will be unavailable while its files are copied, so plan for a short outage.

1. Back up your databases

Before changing anything, make a backup and check that you can use it to restore your data. Keep the backup separate from both the old and new data directories.

2. Check the current location

Run:

sudo mysql -e 'SELECT @@datadir'

This shows where MySQL currently stores its files. The steps below assume the result is /var/lib/mysql. If it is different, use the path shown in place of /var/lib/mysql in the commands below.

Make sure your disk is mounted at the parent directory of your chosen destination. In this example, that directory is /mnt/data. Set the disk to mount automatically when the server restarts. Check the mount, substituting your own path if needed:

findmnt -T /mnt/data

3. Stop MySQL and copy the files

Stop MySQL so its files do not change while they are being copied:

sudo systemctl stop mysql

Create the new directory and copy the files. Replace /mnt/data/mysql with your chosen destination:

sudo install -d -o mysql -g mysql -m 750 /mnt/data/mysql
sudo rsync -aHAX /var/lib/mysql/ /mnt/data/mysql/

The first command creates /mnt/data/mysql and gives the mysql system user permission to use it. In the second command, rsync copies the contents of the old directory into the new one. The trailing slash after /var/lib/mysql/ means “copy the contents of this directory.”

If rsync is not installed, install it with sudo apt install rsync, then run the copy command again. Check that the command finishes without errors before moving on.

4. Tell MySQL about the new location

Open MySQL’s configuration file:

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

Find the [mysqld] section and add or update the datadir line beneath it:

[mysqld]
datadir = /mnt/data/mysql

If the file already has a datadir line, edit that line instead of adding another. Save and close the file.

5. Allow MySQL to use the new directory

Ubuntu’s security settings may prevent MySQL from opening files outside /var/lib/mysql. Try starting MySQL first:

sudo systemctl start mysql

If it starts, go to the next step. If it does not, check the service log:

sudo journalctl -u mysql -n 50 --no-pager

Look for a message saying access to /mnt/data/mysql was denied. Ubuntu may be blocking access through AppArmor, its application security system. In that case, add the new path to MySQL’s local AppArmor rules, then reload the MySQL AppArmor profile. The profile is usually /etc/apparmor.d/usr.sbin.mysqld; local rules are usually kept in /etc/apparmor.d/local/. The exact file and reload command can vary, so check the profile installed on your server. Do not disable the whole profile or make the data directory accessible to everyone.

If the log points to a systemd restriction instead, inspect the service settings with sudo systemctl cat mysql. Add a service override that permits writing to /mnt/data/mysql, while keeping the other security settings enabled.

6. Confirm the move

Check that MySQL is running and using the new directory:

sudo systemctl status mysql --no-pager
sudo mysql -e 'SELECT @@datadir'

The result should show /mnt/data/mysql/. Also check that your applications can connect and see their databases.

Keep the old files for now

Leave /var/lib/mysql in place until MySQL and your applications are working from the new location and you have made a fresh backup. If something goes wrong, stop MySQL, change datadir back to /var/lib/mysql, and start MySQL again.

Once everything is working, you can remove the old copy to reclaim disk space. Double-check the path before deleting anything: removing the wrong directory can permanently delete your database files.

Daniel Opitz

Daniel Opitz

Developer, Trainer & Open Source Contributor. Passionate about PHP, Slim Framework, and clean code.

Comments

↑ Back to Blog