Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / Linux / How to Configure MariaDB Master-Master/Slave Replication

March 11, 2024 LinuxQuestions and Answers

How to Configure MariaDB Master-Master/Slave Replication

Replication in SQL databases is the process of copying data from the source database to another one (or multiple ones) and vice versa. Data from one database server are constantly copied to one or more servers. You can use replication to distribute and balance requests  across a pool of replicated servers, provide failover and high availability of MariaDB databases. The  MariaDB (and MySQL) allows to use two types database replication mades: Master-Master and Master-Slave. In this article we’ll consider how to configure both types of MariaDB replication on Linux CentOS 7.

Contents:
  • Installing MariaDB
  • Configuring Simple Master-Master Replication on MariaDB
  • How to Set Up Master-Slave Replication in MariaDB?

Installing MariaDB

Earlier we published an article describing the process of MariaDB installation on CentOS 7. So we won’t focus on the MariaDB installation itself, but move on to how to configure the replication.

Configuring Simple Master-Master Replication on MariaDB

In a Master-Master replication scheme, any of the MariaDB/MySQL database servers may be used both to write or read data. Replication is based on a special binlog file, a Master server saves all operations with the database to. A Slave server connects to the Master and applies the commands to its databases.

1. MariaDB: Configuration of the First Master Server (Master-1)

Add the following lines to my.cnf file on your first MariaDB server:

#replication
server_id = 1
report_host = master
log_bin = /var/lib/mysql/mariadb-bin
log_bin_index = /var/lib/mysql/mariadb-bin.index
relay_log = /var/lib/mysql/relay-bin
relay_log_index = /var/lib/mysql/relay-bin.index

mariadb innodb master master replication

service mariadb restart

Create a user to configure replication:

mysql
create user 'test_master'@'%' identified by 'test_master';
grant replication slave on *.* to 'test_master'@'%';

To add a Slave, we need to get bin_log data from the Master-1 server:

MariaDB [(none)]> show master status;

+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mariadb-bin.000002 | 664 | | |
+--------------------+----------+--------------+------------------+
1 row in set (0.000 sec)

mariadb get bin_log for replication partner

2. MariaDB: Configuration of the Second Master Server (Master-2)

Connect to the second MariaDB server, open the my.cnf file and add the following configuration to it:

#replication
server_id = 2
report_host = master2
log_bin = /var/lib/mysql/mariadb-bin
log_bin_index = /var/lib/mysql/mariadb-bin.index
relay_log = /var/lib/mysql/relay-bin
relay_log_index = /var/lib/mysql/relay-bin.index

configure replication partner on mariadb

Create a new user on the second server as well:

create user 'test_master2'@'%' identified by 'test_master2';
grant replication slave on *.* to 'test_master2'@'%';

Get bin_log on Master-2:

MariaDB [(none)]> show master status;

+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mariadb-bin.000001 | 667 | | |
+--------------------+----------+--------------+------------------+
1 row in set (0.000 sec)

Let’s configure the connection between MariaDB servers in our software replication cluster:

Stop the slave:

STOP SLAVE;

Add Master-1 to the second server:

CHANGE MASTER TO MASTER_HOST='IP_master1', MASTER_USER='test_master', MASTER_PASSWORD='test_master', MASTER_LOG_FILE='mariadb-bin.000002', MASTER_LOG_POS=664;

Start the replication:

START SLAVE;

get mariadb replication status

Connect to Master-1 and follow the same steps, but specify the information about the second server instead:

STOP SLAVE;
CHANGE MASTER TO MASTER_HOST='10.2.10.36', MASTER_USER='test_master2', MASTER_PASSWORD='test_master2', MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=667;
START SLAVE;

Check the second server status:

show slave status \G

show slave status

As you can see in the screenshots, there is the connection between two servers, and no errors occur.

3. How to Check Replication Between MariaDB Servers?

Then to make sure that the replication between two MariaDB servers works in master+master, we will create a new database on Master-1 and create a table in it.

MariaDB [(none)]> create database master1;
MariaDB [(none)]> use master1;
MariaDB [master1]> CREATE TABLE hello (
-> AuthorID INT NOT NULL AUTO_INCREMENT,
-> AuthorName VARCHAR(100),
-> PRIMARY KEY(AuthorID)
-> );

testing mariadb master replication

Make sure that this database has automatically replicated on the second master and contains the same table:

MariaDB [(none)]> show databases;

+--------------------+
| Database |
+--------------------+
| information_schema |
| master1 |
| mysql |
| performance_schema |
+--------------------+

MariaDB [(none)]> use master1;
MariaDB [master1]> show tables;

+-------------------+
| Tables_in_master1 |
+-------------------+
| hello |
+-------------------+

The database has been created on the second master as well. To check the full cycle, create a table in the Master1 database on the second Master server and check if it appears on the first server.

MariaDB [master1]> CREATE TABLE hello_master1 (
-> AuthorID INT NOT NULL AUTO_INCREMENT,
-> AuthorName VARCHAR(100),
-> PRIMARY KEY(AuthorID)
-> );

The hello_master1 table has been replicated to the first server:

MariaDB [master1]> show tables;

+-------------------+
| Tables_in_master1 |
+-------------------+
| hello |
| hello_master1 |
+-------------------+

show replicated tables in mariadb

As you can see, the new table has appeared on Master-1. The replication works.

How to Set Up Master-Slave Replication in MariaDB?

In the master-slave MariaDB replication mode, one server acts as a slave, and data from the Master server are constantly written to it. All changes you make on the Slave server won’t be transferred to the Master server. This database replication type is more resilient and used more often. In this configuration, you always have a backup server with up-to-date database, and if a slave server fails, the Master will not lost any data. You can also distribute the database load for your project so that the apps will read data from your Slave servers and write the data only to the Master server. Thus, you make the database response as fast as possible.

When configuring the master+slave replication of MariaDB database, the master server (master1) is configured as described above.

Move on to the slave server. Add the following configuration to the slave my.cnf file:

#replication
server_id = 2
report_host = slave2
log_bin = /var/lib/mysql/mariadb-bin
log_bin_index = /var/lib/mysql/mariadb-bin.index
relay_log = /var/lib/mysql/relay-bin
relay_log_index = /var/lib/mysql/relay-bin.index

Restart the MariaDB service. Get the bin_log info from the first server:

MariaDB [(none)]> show master status;

+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mariadb-bin.000001 | 664 | | |
+--------------------+----------+--------------+------------------+

Run these commands in the mariadb console on slave server:

MariaDB [(none)]> STOP SLAVE;

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='IP_master', MASTER_USER='test_master', MASTER_PASSWORD='test_master', MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=664;
MariaDB [(none)]> START SLAVE;

When you configure replication for the existing MariaDB database, you must put the database to the read-only mode prior starting the replication in order bin_log number not to be updated.

SET GLOBAL read_only = ON;

You must also create the database memory dump and use it for initial upload of data to MariaDB on your slave server.

To check the Slave status: SHOW SLAVE STATUS\G;

To create a database on the Master:

MariaDB [(none)]> create database master_hello;

Make sure that the database has been created on the Slave server as well:

MariaDB [(none)]> show databases;

+--------------------+
| Database |
+--------------------+
| information_schema |
| master_hello |
| master_test |
| mysql |
| performance_schema |
| test |
+--------------------+

show database

Create a database on the Slave server and check if the database has been replicated to the Master.

add mariadb database for replication to slave

As you can see, we have created the database and it exists on the Slave. Let’s check if it has appeared on the Master. It has not. The data are not replicated from slave to master.

test master slave replication

It means that MariaDB replication works in one direction only. Let’s do another check and delete the master_hello database from the Slave server:

check slave to master replication

Check if it has deleted from the Master server:

one way replication in mariadb

As you can see, it is OK and the database exists.

P. S.: When you configuring mariadb replication, you may come across some pitfalls, with the firewall as the most frequent of them. By default, the firewalld is installed in CentOS, which blocks the MariaDB replication port (TCP 3396). You can either open the port using iptables, or disable your firewall (not the best option).

The IP address at which the database is waiting for a connection is specified in the bind-address parameter in my.cnf.. To allow local and external connections, you have to comment this line and add the iptables rule that allows connections from the master/slave IP address to port 3306.

iptables -I INPUT -p tcp -s ip_address_slave_server --dport 3306 -j ACCEPT
iptables -I INPUT -p tcp --dport 3306 -j DROP

During the initial setup, I came across this issue, but it is detected easily. If you run the Slave status check SHOW SLAVE STATUS\G, you will see this error:

replication error: cant connect to mysql server - closed ports

You can add some parameters to the #replication block in my.cnf file. Below I will give some examples and a brief description of the useful parameters. I’ll give some examples of other functions that may be helpful when you configure the mariadb database replication.

server_id = 1 — specify the server ID, usually we start with 1, but you may use any number that will not match with those of other servers used for replication.

report_host = master — usually you specify the host name of the server, but you can enter its IP address instead.

log_bin = /var/lib/mysql/mariadb-bin — is the path to the update log (log_bin).

log_bin_index = /var/lib/mysql/mariadb-bin.index — allows you to find out, which log is currently active and which logs were used earlier.

relay_log = /var/lib/mysql/relay-bin

relay_log_index = /var/lib/mysql/relay-bin.index — are the replication logs.

If you want to configure the replica of a specific database or selected databases, use the following parameters:

replicate-do-db = dbname — if you need multiple databases, specify their names separated by commas.

To exclude some databases from replication (as usual, service databases are excluded from replication: information_schema, mysql and performance_schema):

binlog-ignore-db = dbname

To set the log history time for your bin_log:

expire_logs_days = 10 — where 10 is the number of days the logs will be stored.

Also, if the data from your Master server are written to a database with a different name, you can set it in the config file:

replicate-rewrite-db=dbmastername->dbslavename

The replication configuration is completed. I think that using this article you will be able to configure MariaDB replication both in Master + Master and Master + Slave mode without any problems.

9 comments
5
Facebook Twitter Google + Pinterest
previous post
Create & Manage DNS Zones and Records with PowerShell
next post
Get-ADDomainController: Getting Domain Controllers Info via PowerShell

Related Reading

Configure NTP Time Source for Active Directory Domain

May 6, 2025

How to Cancel Windows Update Pending Restart Loop

May 6, 2025

View Windows Update History with PowerShell (CMD)

April 30, 2025

Remove ‘Your License isn’t Genuine’ Banner in MS...

April 21, 2025

Uninstalling Windows Updates via CMD/PowerShell

April 18, 2025

9 comments

LinuxCuba June 26, 2020 - 3:43 pm

Not is server-id, is server_id, please fix it.
Thank you.
Best regards

Reply
admin July 28, 2020 - 4:50 am

Fixed, thanks!

Reply
Barani Kannan Subramanian September 2, 2020 - 1:50 pm

Not fixed, still its server-id. Please update.

Reply
admin September 3, 2020 - 11:38 am

+

Reply
Manna July 20, 2020 - 10:45 am

Good document for those who are doing it first time.

Reply
jagdish November 24, 2020 - 9:04 am

Dear Team,
I have configure MariaDB Master-Master in centos7 as per our instruction , new database replication done successfully but my old database changes not replication, how can resolve this issue ?

Reply
user123 August 11, 2021 - 10:35 am

Hello,

How we can verify whether this two mariadb/mysql is having same data?

Reply
wtf September 30, 2022 - 2:35 pm

just do a diff of your database files or from a dump of them?

Reply
emre ors September 29, 2023 - 8:00 pm

thank you very much for the document. it solved my business. i am grateful to you.

Reply

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMware
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025
  • How to Write Logs to the Windows Event Viewer from PowerShell/CMD

    March 3, 2025
  • How to Hide (Block) a Specific Windows Update

    February 25, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • KVM: How to Expand or Shrink a Virtual Machine Disk Size?
  • Ubuntu/Mint/Kali Boots to Initramfs Prompt in BusyBox
  • How to Install and Use ClamAV Antivirus on CentOS/RHEL
  • Configuring High Performance NGINX and PHP-FPM Web Server
  • Install and Configure SNMP on RHEL/CentOS/Fedor
  • Compress, Defrag and Optimize MariaDB/MySQL Database
  • Configuring Routing on Linux (RHEL/CentOS)
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top