MySQL
data directory
database configuration
server administration
file system paths

MySQL data directory location

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The MySQL data directory is the filesystem location where MySQL stores database files, logs, and internal metadata. You should not guess this path from memory because it varies by operating system, packaging method, container layout, and custom configuration.

The Fastest Way to Find the Real Directory

The most reliable answer comes from the running server itself. Query MySQL for the datadir variable instead of relying on default installation paths.

sql
SHOW VARIABLES LIKE 'datadir';
SELECT @@datadir;

Either query returns the directory MySQL is currently using. This is better than memorizing examples such as /var/lib/mysql because administrators often move the directory to another disk or mount point.

If you cannot connect with a SQL client yet, the server binary can sometimes show the configured default:

bash
mysqld --verbose --help | grep '^datadir'

That is a useful fallback, but the running server remains the authoritative source.

Why the Path Varies

The default location depends on how MySQL was installed.

Typical examples include:

  • Linux packages often use /var/lib/mysql
  • local development installs may use another vendor-specific path
  • Docker images often map the data directory to /var/lib/mysql inside the container, while the real host storage lives in a volume
  • custom deployments may point datadir to a dedicated disk such as /data/mysql

This is why the right operational question is not "what is the default path," but "what path is this server using right now?"

How to Read the Configuration

MySQL can also be configured through my.cnf or my.ini. If the directory was customized, you will often see a datadir setting under the server section.

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

Reading the config is useful for change management, but remember that configuration discovery can be messy when several config files are included. Again, checking @@datadir avoids ambiguity.

Moving the Data Directory Safely

Sometimes you need to move the directory to a larger or faster disk. The safe process is usually:

  1. stop MySQL cleanly
  2. copy the existing data files to the new location
  3. update the datadir setting
  4. fix permissions and security policy
  5. start MySQL and confirm @@datadir

A simplified Linux example looks like this:

bash
sudo systemctl stop mysql
sudo rsync -a /var/lib/mysql/ /data/mysql/
sudo chown -R mysql:mysql /data/mysql

Then update the config and restart the service. After restart, connect and verify:

sql
SELECT @@datadir;

Do not delete the old directory until you have confirmed the server started cleanly and the data is intact.

Permissions and Security Matter

A directory move is not only a filesystem change. MySQL must be able to read and write the new location, and the operating system security policy must allow it.

On Linux, watch for:

  • ownership, usually mysql:mysql
  • directory permissions
  • 'AppArmor profiles on Debian or Ubuntu'
  • 'SELinux contexts on Red Hat based systems'

These platform controls are a common reason a moved data directory fails to start even when the files were copied correctly.

Containers Change the Picture

In containers, the path inside the container may stay constant while the real host storage moves to a Docker volume, bind mount, or Kubernetes persistent volume. For example, @@datadir may still show /var/lib/mysql, but that directory is backed by external storage.

That is normal. The important distinction is:

  • MySQL sees the in-container path
  • your platform manages where that path is persisted

Common Pitfalls

The most common mistake is confusing the configuration directory with the data directory. my.cnf tells MySQL what to do, but it is not where the database files live.

Another mistake is moving files without preserving ownership or updating security policy. MySQL then fails on startup and the path change looks more mysterious than it really is.

A third pitfall is assuming a containerized MySQL instance stores data inside the container image. Production setups usually persist it in a volume.

Summary

  • Use SHOW VARIABLES LIKE 'datadir' or SELECT @@datadir to find the real data directory.
  • Do not rely on default paths because packaging and custom configuration vary.
  • If you move the directory, stop MySQL first and preserve ownership and permissions.
  • Check AppArmor, SELinux, and volume mappings when the new path does not work.
  • In containers, the visible MySQL path may be stable while the host storage lives elsewhere.

Course illustration
Course illustration

All Rights Reserved.