How to change MySQL data directory?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Moving the MySQL data directory is a storage and operations task, not just a config edit. To do it safely, stop MySQL, copy the existing data with permissions intact, update the server configuration to point at the new location, fix any security policy such as AppArmor or SELinux, and then start the server and verify that it is using the new path.
Why You Might Move The Data Directory
Common reasons include running out of space on the current disk, moving database files to faster storage, or separating OS files from database files for operational clarity. Whatever the reason, treat the move as an infrastructure change that can make the server unavailable if handled carelessly.
Before doing anything else, take a backup and confirm you can restore it.
Typical Linux Procedure
The exact file paths vary by distribution, but the workflow is consistent. Suppose you want to move the data directory from /var/lib/mysql to /data/mysql.
First, stop the service:
Create the target directory and copy the data while preserving ownership, permissions, and symlinks:
Then update ownership if needed:
At this point, do not delete the old directory yet. Keep it as a rollback option until the new location is confirmed working.
Update MySQL Configuration
Edit the MySQL configuration file and set the new datadir. Depending on the system, this may be in /etc/mysql/mysql.conf.d/mysqld.cnf, /etc/my.cnf, or another included file.
A typical section looks like this:
If your server uses relative paths for logs, temporary files, or InnoDB settings, review those too. The data directory move may expose assumptions elsewhere in the config.
Security Policy Adjustments
On Ubuntu and Debian systems, AppArmor may block MySQL from reading the new directory. Add the new path to the MySQL AppArmor profile:
Then reload the profile:
On systems with SELinux, the new directory may need the proper context instead:
If you skip this step, MySQL may fail to start even though the configuration file looks correct.
Start MySQL And Verify
Start the service again:
Then verify the active data directory from inside MySQL:
You should also check the error log and confirm that your databases are visible:
Only after that verification should you consider archiving or removing the old directory.
Notes For Windows
On Windows, the same principles apply:
- Stop the MySQL service.
- Copy the data directory to the new location.
- Update
my.iniwith the newdatadir. - Ensure the Windows service account has permission to the new folder.
- Restart the service and verify the change.
The exact service commands and file paths differ, but the operational order is the same.
Common Pitfalls
The biggest mistake is moving the files while MySQL is still running. That risks a partial copy and inconsistent data files.
Another common issue is forgetting security policy changes. AppArmor and SELinux often block the new path even when file ownership is correct.
Developers also sometimes edit the wrong config file. MySQL installations can load settings from multiple locations, so verify the effective config path on your system.
Finally, do not delete the old data directory until you have started MySQL successfully, confirmed the new path with SHOW VARIABLES, and checked the error log for storage-engine problems.
Summary
- Stop MySQL before moving the data directory.
- Copy the existing data to the new location with permissions preserved.
- Update
datadirin the active MySQL configuration. - Fix AppArmor, SELinux, or Windows permissions for the new path.
- Restart MySQL and verify the new directory before cleaning up the old one.

