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

ما هو حقن التبعيات (Dependency Injection)؟

class Logger {
    public function log($message) {
        echo $message;
    }
}

class UserService {
    protected $logger;

    public function __construct(Logger $logger) {
        $this->logger = $logger;
    }

    public function create() {
        $this->logger->log("User created.");
    }
}

يتم تمرير التبعية عند استدعاء وظيفة معينة تحتاج إليها.

Dec 01, 2025
Read More
Article

أفضل طرق إزالة الصدأ من العدّة والمسامير – دليل شامل منزلي واحترافي

  • مثالي لـ 100 مسمار في المرة الواحدة.
  • سهل الإعداد ولا يحتاج أدوات إضافية.
  • استخدم سلك نحاسي قوي أو مسمارًا طويلًا.
  • أدخل المسامير حوله كما لو كانت "خرز".
  • اربطها بسلك واحد من الأعلى.
  • وصّل السالب بالسلك الرئيسي.

Dec 01, 2025
Read More
Tutorial

How to Stop SSH From Timing Out

On your local machine, edit or create:

nano ~/.ssh/config

Aug 21, 2025
Read More
Tutorial

How to Translate URLs in React (2025 Guide)

import React from 'react';
import { useTranslation } from 'react-i18next';
import { BrowserRouter, Routes, Route, useNavigate } from 'react-router-dom';
import { routes } from './routes';
import './i18n';

const LanguageSwitcher = () => {
  const { i18n } = useTranslation();
  const navigate = useNavigate();

  const switchLang = (lang) => {
    const currentPath = window.location.pathname;
    const currentPage = currentPath.split('/')[1];

    i18n.changeLanguage(lang).then(() => {
      // Re-map path using new language
      const t = i18n.getFixedT(lang);
      const mappedRoutes = {
        en: { accueil: 'home', 'a-propos': 'about-us' },
        fr: { home: 'accueil', 'about-us': 'a-propos' },
      };

      const newPath = `/${mappedRoutes[lang][currentPage] || ''}`;
      navigate(newPath);
    });
  };

  return (
    <div className="lang-switch">
      <button onClick={() => switchLang('en')}>EN</button>
      <button onClick={() => switchLang('fr')}>FR</button>
    </div>
  );
};

const App = () => {
  const { t } = useTranslation();

  return (
    <BrowserRouter>
      <LanguageSwitcher />
      <Routes>
        {routes(t).map((route, idx) => (
          <Route key={idx} path={route.path} element={route.element} />
        ))}
      </Routes>
    </BrowserRouter>
  );
};

export default App;

Create pages/Home.js:

May 04, 2025
Read More

Discussion 0

Please sign in to join the discussion.

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