How to Change location Of MariaDB Data Directory

How to change location Of MariaDB Data Directory Safely. Here is the steps to follow migrate data directory to another server. read more: How to check partition details of MySQL table Know more about Change default MySQL Data Directory in Linux

You may require to change the MySQL data directory due to insufficient partition space on /var or /root and change the data directory to the /home for your server. However, there might also be other reasons for changing the data directory.

By default, MySQL/ MariaDB data directory is /var/lib/mysql. It stores all the databases to /var/lib/mysql

Please refer to the following steps to change the MySQL Data Directory to /home.  

  1. Take backup of existing folder:

    tar -cvf mysql.tar /var/lib/mysql
  2. Stop the mariadb services

    systemctl stop mariadb
  3. Syncing data directory to new location/directory

    Now, we will sync all the MySQL databases to the /home/mysql. The following command will create a mysql directory in /home and start the sync process.

    rsync -avz /var/lib/mysql to /home
  4. Change the data directory

    To change MySQL/ MariaDB data directory, edit the /etc/my.cnf file with your preferred editor.

    vi /etc/my.cnf

    Change the data directory from /var/lib/mysql to the /home.
    If you don’t find an existing line of datadir, you can add a new line as mentioned below –

    datadir=/home/mysql
  5. Change in Daemon services

    To run MariaDB Service from /home, you need to modify the mariadb.service file at the /usr/lib/systemd/system/mariadb.service location.

    ProtectHome=true to ProtectHome=false
  6. Reload Daemon

    After Changing in the daemon file, need to reload the file as follows:

    systemctl daemon-reload
  7. Start mariadb services

    Finally, start your MariaDB service. 

    systemctl start mariadb

Read More: Install MariaDB 10.6 on CentOS 7

Leave a Reply