Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

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

April 19, 2023 CentOSLinuxQuestions 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
2
Facebook Twitter Google + Pinterest
previous post
Control Panel Mail App Not Found on Windows 10
next post
Get-ADDomainController: Getting Domain Controllers Info via PowerShell

Related Reading

How to Increase Size of Disk Partition in...

October 5, 2023

How to Use Ansible to Manage Windows Machines

September 25, 2023

Fixing ‘The Network Path Was Not Found’ 0x80070035...

August 30, 2023

How to Install and Configure Ansible on Linux

August 27, 2023

Computer Doesn’t Turn Off After Shutting Down Windows...

August 26, 2023

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

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

  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023
  • How to Use Ansible to Manage Windows Machines

    September 25, 2023
  • Installing Language Pack in Windows 10/11 with PowerShell

    September 15, 2023
  • Configure Email Forwarding for Mailbox on Exchange Server/Microsoft 365

    September 14, 2023
  • How to View and Change BIOS (UEFI) Settings with PowerShell

    September 13, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • How to Mount Google Drive or OneDrive in Linux?
  • KVM: How to Expand or Shrink a Virtual Machine Disk Size?
  • Configuring High Performance NGINX and PHP-FPM Web Server
  • Adding VLAN Interface in CentOS/Fedora/RHEL
  • Install and Configure SNMP on RHEL/CentOS/Fedor
  • Configuring Routing on Linux (RHEL/CentOS)
  • Compress, Defrag and Optimize MariaDB/MySQL Database
Footer Logo

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


Back To Top