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

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


Start MariaDB service and enable it on boot

# systemctl start mariadb
# systemctl enable mariadb

Sample Output


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


The reload the firewall to effect the changes

# firewall-cmd --relaod

Sample Output

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;

Next, create a Slave user and password. For example, we will use slave_user as Slave username and [email protected] 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 '[email protected]';
Query OK, 0 rows affected (0.00 sec)

Next, Flush the privileges as shown

Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)


Next, execute the command below to display the 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


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


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

So the command will be:

scp masterdatabase.sql [email protected]:/home

Please recall that is our MariaDB slave server.

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='[email protected]', 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)

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



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

By admin

Leave a Reply

%d bloggers like this: