Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Databases»MariaDB»How to Change default MySQL data Directory in Linux

    How to Change default MySQL data Directory in Linux

    RahulBy RahulMay 1, 20142 Mins ReadUpdated:October 1, 2021

    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 command as per you 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:
        1
        2
        datadir=/var/lib/mysql
        socket=/var/lib/mysql/mysql.sock

        Change To:
        1
        2
        datadir=/data/mysql
        socket=/data/mysql/mysql.sock
    4. Start MySQL – After making all the above changes, start MySQL service. Now it will use 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.

    datadir dbpath mariadb MySQL
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleManage Amazon S3 using Windows Command Utility (s3.exe)
    Next Article How to Create and Drop database in MongoDB

    Related Posts

    Backup MySQL Databases to Amazon S3 (Shell Script)

    Updated:May 28, 20222 Mins Read

    How to Install MariaDB on Ubuntu 22.04

    Updated:May 28, 20222 Mins Read

    How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04

    Updated:April 7, 20227 Mins Read

    How To Install MySQL Server on Ubuntu 22.04

    Updated:April 6, 20224 Mins Read

    How To Install LAMP Stack on Ubuntu 22.04 LTS

    Updated:April 20, 20225 Mins Read

    How To Install MariaDB on Debian 11

    4 Mins Read

    11 Comments

    1. Rosel on August 7, 2018 6:46 pm

      After changing data directory, is it necessary to check used mysql ports before restart services?

      Reply
    2. altamash on March 23, 2018 9:04 am

      hi ,
      i did the above procedure in RHEL 7 , the files were copied also i changed the ownership of mysql data directory which has the same permission as the original file but still getting an error when i restart the mysql service
      stating “Job for mysqld.service failed. See ‘systemctl status mysqld.service’ and ‘journalctl -xn’ for details”

      please help thanks!

      Reply
      • Rahul K. on April 5, 2018 3:28 am

        What is output of “systemctl status mysqld.service”?

        Reply
    3. KaZuN on February 29, 2016 11:15 am

      Is any idea how its work on centos 7.cause my mysqld cannot start after done these change.thanks.

      Reply
    4. Jasper vS on December 22, 2015 5:05 pm

      Is there any reason to move the .sock file?

      I mean it can be kept in the original location, right?

      Reply
    5. John on November 25, 2014 9:37 am

      Hi,
      Thanks for the article !
      I’d like to add something because I was facing an issue while trying to connect to the database after I made the changes listed above.
      When I tried to connect to the database with the mysql client, I had this error :
      “ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)”

      The problem where that the mysql client is using by default the /var/lib/mysql/mysql.sock socket.
      In the /etc/my.cnf file, I just added a client section specifying the socket :

      [client]
      socket=/data/mysql/mysql.sock

      Hope this will help,

      Cheers !

      Reply
      • Brenda Yin on April 10, 2015 3:28 am

        John,

        You have to relink the sock file, which is under the new data directory.

        Here is the sample:

        ln -s /data/mysql/mysql.sock /var/lib/mysql/mysql.sock

        Reply
      • Razor on April 28, 2019 6:50 pm

        Thank you it worked for me

        Reply
    6. Darius on November 12, 2014 3:36 am

      Shouldn’t the Change To: datadir=/data/mysql in this example?

      Reply
      • Rahul on November 12, 2014 9:47 am

        Hi Darius,

        Yes, We need to change it. We have corrected typo.

        Reply
    7. sajith on October 4, 2014 2:11 am

      I tried to do the same in CENTOS 6.4 and https://www.centos.org/forums/viewtopic.php?t=6732 this link worked perfectly!

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    • How to Install Angular CLI on Ubuntu 22.04
    • How to Install Composer on Ubuntu 22.04
    • How to Create DMARC Record For Your Domain
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.