Saturday, December 12, 2020

How2 change Datadir path under MariaDB 10.3 running on CentOS 8 server

This tutorial will guide with the steps to change datadir in MariaDB and also provide with rollback steps in case it does not work as expected.

Configuration steps are as follows


    a) Plan for the server downtime and shutdown MariaDB.

            # systemctl stop mariadb

        Reconfirm the status of MariaDB is properly shutdown.

            # systemctl status mariadb


    b) Copy existing data to new datadir (in our case we will use /mnt/data), but replace this path as per your new datadir path.

            # rsync -av /var/lib/mysql /mnt/data

        Optional step : Above rsync command will copy all data files keeping ownership intact, but to ensure all directories and datafiles have correct ownership execute following command

           
# chown -R mysql:mysql /mnt/data/


    c) Rename original datadir so as to have a rollback plan in place and also to avoid confusion.

            # mv /var/lib/mysql /var/lib/mysql.old


    d) Update config files with new datadir path

        Update server config file

            
# vi /etc/my.cnf.d/mariadb-server.cnf

        in [mysqld] section, comment out original datadir and socket lines as it can be used to roll back changes

            
#datadir=/var/lib/mysql
            #socket=/var/lib/mysql/mysql.sock

        and add new values

            datadir=/mnt/data/mysql
            socket=/mnt/data/mysql/mysql.sock
 
        and save the file and proceed to updated client config file

            # vi /etc/my.cnf.d/client.cnf

        under client-mariadb section add this line

            [client-mariadb]
            
socket=/mnt/data/mysql/mysql.sock

        and save the file


    e) Start MariDB server

           
# systemctl start mariadb

        and check if mariadb is working fine

            # systemctl status mariadb

        Incase there is any error then you can try to debug the same or roll back steps as mentioned in section c & d and restart MariaDB and you will be back to original state


    f) To check the status, login into MariaDB console

            # mysql -uroot -p

        check if datadir is set correctly
        
           
> select @@datadir;

        and response should be as follows

            +-------------------+
            | @@datadir         |
            
+-------------------+
            | /mnt/data/mysql/  |
           
+-------------------+


    g) To check everything is working fine, create a database

            
> create database test;

        and if created successfully then proceed to clean up the test database. 

            > drop database test;

        and exit out of mariadb.


    h) After ensuring all the data is successfully copied and new Datadir path is working fine, delete the /var/lib/mysql.old folder to save on hdd space.

            # rm -rf /var/lib/mysql.old

With this we are done with moving Datadir path under MariaDB 10.3 and I hope this post help you with the required steps.

No comments:

Post a Comment