MySQL
Data Directory
Database Management
Server Configuration
MySQL Setup

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:

bash
sudo systemctl stop mysql

Create the target directory and copy the data while preserving ownership, permissions, and symlinks:

bash
sudo mkdir -p /data/mysql
sudo rsync -av /var/lib/mysql/ /data/mysql/

Then update ownership if needed:

bash
sudo chown -R mysql:mysql /data/mysql

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:

ini
[mysqld]
datadir=/data/mysql
socket=/var/run/mysqld/mysqld.sock

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:

text
/data/mysql/ r,
/data/mysql/** rwk,

Then reload the profile:

bash
sudo systemctl reload apparmor

On systems with SELinux, the new directory may need the proper context instead:

bash
sudo semanage fcontext -a -t mysqld_db_t "/data/mysql(/.*)?"
sudo restorecon -Rv /data/mysql

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:

bash
sudo systemctl start mysql
sudo systemctl status mysql

Then verify the active data directory from inside MySQL:

sql
SHOW VARIABLES LIKE 'datadir';

You should also check the error log and confirm that your databases are visible:

sql
SHOW DATABASES;

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.ini with the new datadir.
  • 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 datadir in 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.

Course illustration
Course illustration

All Rights Reserved.