Setup MariaDB Master-Slave Replication In CentOS 7

Why MariaDB Replication?

As you may know, MariaDB is a drop in replacement for MySQL. It is a robust, scalable and reliable SQL server that comes rich set of enhancements.

MariaDB replication is a method to store multiple copies of data on many systems, and the data will automatically be copied from one database (Master) to another database (Slave). If one server goes down, the clients still can access the data from another (Slave) server database.

In this article, let us see how to configure MariaDB Master-Slave replication in CentOS 7. This method will work on all linux distributions, including RHEL, CentOS, Ubuntu, and openSUSE etc. All you need to know is how to install MariaDB in the specific distribution you use.


I will be using the following two systems for the purpose this tutorial. But you can use any Linux distribution to setup MariaDB replication. Only the installation of MariaDB will differ for different Linux Operating Systems. Refer the following link to know more about  MariaDB installation on various Linux distributions.

Here is my testing systems details:

  • MariaDB Master: CentOS 7 64bit Minimal Server
  • Master IP Address:
  • MariaDB Slave: CentOS 7 64bit Minimal Server
  • Slave IP Address:

Install MariaDB on Master and Slave server

Run the following command on Master and Slave server to install MariaDB.

yum install mariadb-server mariadb

Start MariaDB service and let it to start automatically on every reboot:

systemctl start mariadb
systemctl enable mariadb

Set MariaDB root password on Master and Slave server

By default, MariaDB/MySQL root password is empty. So, to prevent unauthorized access, let us set root user password.

Enter the following command to setup mysql root user password:


Sample output:

/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found


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!

After installing MariaDB on both servers, let us jump into the replication setup.

First, we will configure Master server.

Configure MariaDB Master

The first and foremost step is to allow mysql default port “3306” through Firewall or Router.

As we use CentOS 7, we can allow the port as shown below.

firewall-cmd --permanent --add-port=3306/tcp

Reload firewall rules using command:

firewall-cmd --reload

Then, Edit /etc/my.cnf file,

vi /etc/my.cnf

And add the following lines under [mysqld] section:


Here unixmen is the database name to be replicated to the Slave system.

Once you are done, restart the MariaDB service using command:

systemctl restart mariadb

Now login to MariaDB as root user:

mysql -u root -p

Create a Slave user and password. For example, we will use sk as Slave username and centos as password:

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

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

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB |
| mariadb-bin.000001 |      460 | unixmen      |                  |
1 row in set (0.00 sec)

MariaDB [(none)]> exit

Note down the file (mariadb-bin.000001) and position number (460), you may need these values later.

Backup Master server database and transfer it to the Slave

Enter the following command to dump all Master databases and save them. We will transfer these databases to Slave server later:

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

This will create a file called masterdatabase.sql in your current working directory. This will take some time depending upon the databases size.

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

Copy the masterdatabase.sql file to your Slave server.

Here, I am going to copy this file to the /home folder of my slave server. So the command will be:

scp masterdatabase.sql root@

Remember, is my MariaDB slave server IP address.

Configure MariaDB Slave

We have done Master side installation. Now we have to start on Slave side. Install MySQL packages on Slave server as stated in the MariaDB installation section. Also, don’t forget to allow the port “3306” through the firewall/router.

Then, Edit file /etc/my.cnf,

vi /etc/my.cnf

And add the following entries under [mysqld] section as shown below.

server-id = 2

Here, unixmen is the database name of Master server. Be mindful that you should use different server-id for both master and slave servers.

Save and exit the file.

Then, Import the master database using command:

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

Remember, we already have copied the masterdatabase.sql file from the master server to /home/ directory of the slave server.

Restart MariaDB service to take effect the changes.

systemctl restart mariadb

Now, log in to MariaDB as root user using command:

mysql -u root -p

And tell the Slave server to where to look for Master log file which is we have created on Master server using the command SHOW MASTER STATUS; (File –mariadb-bin.000001 and Position – 460). Make sure that you changed the Master server IP address, username and password as your own:

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

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='', MASTER_USER='sk', MASTER_PASSWORD='centos', MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=460;
Query OK, 0 rows affected (0.03 sec)

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

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_User: sk
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 460
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 531
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: unixmen
                   Last_Errno: 0
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 460
              Relay_Log_Space: 827
              Until_Condition: None
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
               Last_SQL_Errno: 0
             Master_Server_Id: 1
1 row in set (0.00 sec)

Test MariaDB Replication

Master side:

Go to your MariaDB master server and log in to mysql prompt using command:

mysql -u root -p

Then, create a database called unixmen, and add some tables and entries in it. Be mindful that the newly created name should be same as in the my.cnf file of both Master and Slave server.

MariaDB [(none)]> create database unixmen;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use unixmen;
Database changed

MariaDB [unixmen]> create table sample (c int);
Query OK, 0 rows affected (0.01 sec)

MariaDB [unixmen]> insert into sample (c) values (1);
Query OK, 1 row affected (0.01 sec)

MariaDB [unixmen]> select * from sample;
| c    |
|    1 |
1 row in set (0.00 sec)

MariaDB [unixmen]> 

Slave side:

Now, go to slave server and check the above created entries have been replicated in slave server database.

Log in to MariaDB prompt as root user:

mysql -u root -p

Then, run the following commands to verify whether the entries have been replicated correctly.

MariaDB [(none)]> use unixmen;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [unixmen]> select * from sample;
| c    |
|    1 |
1 row in set (0.00 sec)

MariaDB [unixmen]>

That’s it. Now the tables created in the Master server have been automatically replicated to the Slave server.

You might want to check our previous article about MySQL master-slave replication in CentOS 6.

Happy weekend!