How to Change a Default MySQL/MariaDB Data Directory in Linux

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.95/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Gabriel Cánepa

Gabriel Cánepa is a GNU/Linux sysadmin and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

4 Responses

  1. Balu says:

    Super job It so helped me And we need to install “yum install policycoreutils-python”
    if we unable to find *semanage* command. It very helped me a lot. Before i done all steps but not getting proper result.
    Because i for got about Selinux group.

  2. Angela Erin says:

    1. Stop MySQL, Before making any changes, first make sure to stop mysql service

    # service mysqld stop
    

    2. Change 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. Now edit MySQL default configuration file /etc/my.cnf and update values of datadir and socket variable.

    Change From:
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    Change To: 
    datadir=/data/mysql
    socket=/data/mysql/mysql.sock
    

    4. Start MySQL, After making all above changes. finally, start MySQL service. Now it will use new data directory path.

    # service mysqld start
    
    • milos says:

      @Angela Erin I did all the steps you described and I get the message: start: Job failed to start

      Any suggestions?
      thanks in advance

      • Gabriel A. Cánepa says:

        Can you please share the output of
        journalctl -xn
        and
        systemctl -l status mariadb
        right after you attempt to restart the service?

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *