How to set up MariaDB Master-Slave replication on CentOS 7

Filed Under: Database

The concept of database replication ensures that data is copied across multiple servers from a Master server. This provides data redundancy and ensures that data is not lost in the event that the Master node fails. In this article, we take a look at MariaDB Master-Slave replication on CentOS 7. We will demonstrate how data can be copied from a database located on a Master node to another database located on a Slave system.

MariaDB Master-Slave Replication Scenario

Here’s the replication Set-up:

Master node (CentOS 7 64 bit) : IP

Slave node: (CentOS 7 64 bit) : IP

Step 1: Install MariaDB on both the Master and Slave node

To start off, log in to both the master and slave node and run the following commands to install MariaDB server

yum install mariadb-server mariadb

Sample Output

Install Maria Db Mariadb

Start MariaDB service and enable it on boot

# systemctl start mariadb
# systemctl enable mariadb

Sample Output

Start And Enable Mariadb Server

Step 2: Set MariaDB password on both Master and Slave

By default, the password for MariaDB/MySQL is usually empty and unauthorized users can access the database. We need to make it secure by configuring a password and hardening it with other few settings. To achieve this run the command below on both the master and the slave node


Sample Output


In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y ## Enter Y and press Enter
New password:   ## Enter new password
Re-enter new password:  ## Enter password again
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y  ## Enter Y and press Enter
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y  ## Enter Y and press Enter
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y  ## Enter Y and press Enter
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y  ## Enter Y and press Enter
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Step 3: Configuring the Master node

Now that we have hardened our MariaDB instances on both nodes, Let’s configure the Master node.

First, we need to allow MariaDB’s port 3306 across the CentOS 7 firewall. To accomplish this, run the commands

# firewall-cmd --add-port=3306/tcp --zone=public --permanent

Sample Output

Allow MySQL Port 3306 On Firewalld

The reload the firewall to effect the changes

# firewall-cmd --relaod

Sample Output

Reload Firewall

Next, make a few changes to the /etc/my.cnf file

vim /etc/my.cnf

append the following lines in the [mysqld] section


Here, replica_db is the database that we are going to create and replicate it across the slave.

Next, restart the MariaDB service using command:

systemctl restart mariadb

Now we are going to login to MariaDB as root user:

mysql -u root -p

The next step will be to Create the replica_db database

MariaDB [(none)]> CREATE DATABASE replica_db;

Create Test Databse

Next, create a Slave user and password. For example, we will use slave_user as Slave username and P@ssword100 as password:

MariaDB [(none)]> STOP SLAVE;
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY 'P@ssword100';
Query OK, 0 rows affected (0.00 sec)

Create Slave User

Next, Flush the privileges as shown

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Flush Privilges With Read Lock

Next, execute the command below to display the master status


Show Master Status

Step 4: Backing up the database in Master server and transferring it to the Slave

Next, run the command below to back up all the Master databases

 # mysqldump --all-databases --user=root --password --master-data > masterdatabase.sql

Backup Databases

This creates a file called masterdatabase.sql in your current working directory.

Again login to MySQL as root user:

mysql -u root -p

And, unlock the tables:

MariaDB [(none)]> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> quit

Unlock Tables in MariaDB

Now Copy the masterdatabase.sql file to your Slave server.

So the command will be:

scp masterdatabase.sql root@

Please recall that is our MariaDB slave server.

Scp Database Backup To Slave

Step 4: Configuring MariaDB Slave

Now it’s time to configure the MariaDB Slave node

Edit file the /etc/my.cnf file

vim /etc/my.cnf

Append the following entries under the [mysqld] section as shown

server-id = 2

Here, replica_db is the database created on the Master Server node. Also, Be mindful to use different server-id for both master and slave servers. In this case, the server-id is 2

Save and exit the file.

Next, we are going to import the master database as shown

mysql -u root -p < /home/masterdatabase.sql 

Bear in mind that we had already copied the masterdatabase.sql file from the master server to /home/ directory of the slave server.

Restart MariaDB service to effect the changes.

systemctl restart mariadb

Now login into MariaDB as the root user

mysql -u root -p

Stop the Slave. Instruct the Slave where to find the Master Log file and start the Slave.

MariaDB [(none)]> STOP SLAVE;

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='', MASTER_USER='slave_user', MASTER_PASSWORD='P@ssword100', MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=473;
Query OK, 0 rows affected (0.03 sec)

MariaDB [(none)]> STOP SLAVE;
Query OK, 0 rows affected (0.01 sec)

Configure Slave

Next, run the command below to show the status of the Slave

MariaDB [(none)]> SHOW SLAVE STATUS\G;

Show Slave Status

Testing MariaDB Replication

Master side:

Head out to your MariaDB master server and log in to MariaDB instance using the command as shown

mysql -u root -p

Create the database replica_db

Create Test Databse

Next, create a table Persons

Create Table Persons

Add a record as shown

Insert Records Into Table

Finally, Display the table

Display Table

Slave side:

Now , log in to MariaDB database instance at the Slave server

mysql -u root -p

Log In MariaDB In Slave

Next, display the databases using the command below


Show Databases

As you can see, the replica_db database is present, implying that it has been replicated !

Let’ go ahead and probe inside the database and check if any tables are present. Run

use replica_db;


show tables;

Show Tables command

As you can see, the table Persons created earlier in the Master node is present. Perfect!

Let’s reveal its records to be dead sure that our database has been fully replicated

select *from replica_db;

Show Contents Of A Table

As observed, all the contents have been replicated and are accurate. Way to go!

In this article, you have learned how to How to set up MariaDB Master-Slave replication on CentOS 7. Give it a try and feel free to weigh in.


  1. massimo says:

    Very clear and concise steps. I use to work with slave configuration, but now I’m going to setup a server finding some issues.
    First thing to understand is the correct syntax, you suggest :

    Looking into phpmyadmin under “Rplication” setting and trying to configure from here it tell me to use

    Modifying the server.cnf using both suggestions has no result in starting server

    This is the status :
    MariaDB [(none)]> status
    mysql Ver 15.1 Distrib 5.5.68-MariaDB, for Linux (x86_64) using readline 5.1

    Connection id: 16
    Current database:
    Current user: root@localhost
    SSL: Not in use
    Current pager: stdout
    Using outfile: ”
    Using delimiter: ;
    Server: MariaDB
    Server version: 5.5.68-MariaDB MariaDB Server
    Protocol version: 10
    Connection: Localhost via UNIX socket
    Server characterset: latin1
    Db characterset: latin1
    Client characterset: utf8
    Conn. characterset: utf8
    UNIX socket: /var/lib/mysql/mysql.sock
    Uptime: 16 min 9 sec

    Threads: 1 Questions: 144 Slow queries: 0 Opens: 7 Flush tables: 3 Open tables: 5 Queries per second avg: 0.148

    MariaDB [(none)]> SHOW DATABASES;
    | Database |
    | information_schema |
    | db1 |
    | easy_forms |
    | m26 |
    | mysql |
    | performance_schema |
    | phpblog |
    | test |

    MariaDB [(none)]> SHOW MASTER STATUS;
    Empty set (0.00 sec)

    Any idea ? Thank you

    1. sandeep says:

      Check whether u have enter the configuration under [Mysqld] or [Mysqld_safe].

  2. yourserver says:

    Thank you so much. This is one complete and easy to follow tutorial.
    Best regards

  3. TechGuy says:

    Very nice article!
    How to switch the role in case of Master crash or Upgrade activity. I mean graceful and forceful (cold /hot) switch.

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors