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

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

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.45/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...

7 Responses

  1. dalu says:

    The new version 10.1.15 of the mariadb /etc/my.cnf does not have the datadir. below the what is in the file? Where to the change?

    # This group is read both both by the client and the server
    # use it for options that affect everything
    # include all files from the config directory
    !includedir /etc/my.cnf.d
  2. Artur says:

    Hi, I had the same problem (Job failed to start).

    log: Database MariaDB is not initialized, but the directory /home/data is not empty, so initialization cannot be done.

    I did something like that:

    1. copy all mysql/mariadb datadir into the: /home/data/mysql
    2. mv /var/lib/mysql /var/lib/mysql.bak
    3. mkdir /var/lib/mysql
    4. mount –bind /home/data/mysql /var/lib/mysql (and save/remember it in fstab)

    And It’s working!

  3. 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.

  4. 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:
    Change To: 

    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
        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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.