Home » How To » How to Change Default MySQL Data Directory in Linux

How to Change Default MySQL Data Directory in Linux

MySQL is a popular relational database management system (RDMS). It is widely used by web applications for storing data permanently. MySQL is available for all the major operating systems like Windows, Linux, and macOS. You can find the installation instructions here.

On the Linux system, MySQL stores all the files under /var/lib/mysql directory. Most of the operating systems have mounted /var directory on root (/) file system. This is fine for the development systems but we don’t recommend keeping/var/lib/mysql on the root file system for production servers.

In this tutorial, we will help you to change the default data directory for MySQL and move it to some other location.

Steps to Change Default Data Directory in MySQL

Follow the below steps to make all the changes. In some cases service name, default data directory or MySQL configuration file path change. So use all the commands as per your system settings.

  1. Stop MySQL – Before making any changes, first, make sure to stop mysql service
    sudo systemctl stop mysql 
  2. Copy Data Directory – Now copy default MySQL data directory (/var/lib/mysql) to other location as per your requirement. Also set the required MySQL ownership on new directory location. As per below command, we are relocating data directory to /data/mysql.
    cp -rap /var/lib/mysql /data/mysql
    chown mysql.mysql /data/mysql 
  3. Update configuration file – Edit MySQL configuration file /etc/my.cnf and update the value of datadir and socket variable as below.
    Change From:


    Change To:

  4. Start MySQL – After making all the above changes, start the MySQL service. Now it will use a new data directory path
    sudo systemctl start mysql

That’s it. With the help of help instructions, you will easily change the default data directory location for MySQL and MariaDB servers on Linux systems. Feel free to use the comment section for any suggestion 😎

Leave a Comment