DeveloperBreeze

How to Move the MySQL Data Directory to a New Location on Ubuntu 25.04

By default, MySQL stores all its data in /var/lib/mysql. This tutorial explains how to safely move the MySQL data directory to a new location (e.g., another disk or partition) on Ubuntu 25.04.

Step 1: Stop the MySQL Service

Before making any changes, stop the MySQL service:

sudo systemctl stop mysql

Step 2: Move the Existing Data Directory

Choose your new location, for example /mnt/data/mysql, and move the current data:

sudo mv /var/lib/mysql /mnt/data/mysql

> This moves all database files including system schemas like mysql and performance_schema.


Step 3: Set Correct Permissions

Ensure MySQL can access the new directory:

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

Step 4: Update MySQL Configuration

Edit the MySQL configuration file:

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

Find the line:

datadir = /var/lib/mysql

Change it to:

datadir = /mnt/data/mysql

Save and exit.


Step 5: (If AppArmor is Enabled) Add Access Rules

AppArmor may block MySQL from accessing the new path.

Edit AppArmor profile for MySQL:

sudo nano /etc/apparmor.d/usr.sbin.mysqld

Add these lines before the closing }:

/mnt/data/mysql/ r,
/mnt/data/mysql/** rwk,

Then reload AppArmor:

sudo apparmor_parser -r /etc/apparmor.d/usr.sbin.mysqld

Step 6: Fix MySQL Socket Directory (Optional)

If needed, recreate the MySQL socket directory:

sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld

Step 7: Restart MySQL

sudo systemctl start mysql

Step 8: Verify the New Data Directory

Check that MySQL is using the new path:

mysql -u root -p -e "SHOW VARIABLES LIKE 'datadir';"

You should see:

+---------------+------------------+
| Variable_name | Value            |
+---------------+------------------+
| datadir       | /mnt/data/mysql/ |
+---------------+------------------+

Related Posts

More content you might like

Tutorial

How to Stop SSH From Timing Out

If your SSH session closes after a minute of inactivity, it’s usually caused by idle timeouts. You can fix this with keep-alive settings on both the server and client.

Edit the SSH daemon config:

Aug 21, 2025
Read More
Tutorial

How to Translate URLs in React (2025 Guide)

import { useTranslation } from 'react-i18next';

export default function About() {
  const { t } = useTranslation();
  return (
    <div>
      <h1>{t('routes.about')}</h1>
    </div>
  );
}

For full SEO benefits in translated URLs:

May 04, 2025
Read More
Tutorial

Globalization in React (2025 Trends & Best Practices)

°C vs °F, km vs miles, 12h vs 24h clock.

  • Avoid idioms/slang in content

May 04, 2025
Read More
Tutorial

Implementing Internationalization (i18n) in a Large React Application (2025 Guide)

import ICU from 'i18next-icu';

i18n
  .use(ICU) // Enables datetime and currency formatting
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    ...
    interpolation: {
      format: (value, format, lng) => {
        if (format === 'datetime') {
          return new Intl.DateTimeFormat(lng).format(value);
        }
        if (format === 'currency') {
          return new Intl.NumberFormat(lng, {
            style: 'currency',
            currency: lng === 'fr' ? 'EUR' : 'USD',
          }).format(value);
        }
        return value;
      },
    }
  });

In large apps, structure your translation files modularly:

May 04, 2025
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!