Bobcares

MariaDB Master-Slave Replication

by | May 5, 2022

MariaDB Master-Slave replication ensures that data from a Master server is replicated across many servers. We’ll look at how data can be moved from one database on a Master node to another on a Slave system.

As part of our Server Management services, Bobcares provides solutions for every query.

Take a look at how our Server administration team handles MariaDB Master-Slave replication.

MariaDB Master-Slave replication Step by Step

Data from a Master server is replicated across several MariaDB servers using Master-Slave replication. This ensures data redundancy and prevents data loss if the Master node goes down.

The replication setup is as follows:

Master node (CentOS 7 64 bit) : IP 173.82.2.236
Slave node: (CentOS 7 64 bit) : IP 173.82.94.57

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

To begin, log in to both the master and slave nodes and run the MariaDB server installation commands.

yum install mariadb-server mariadb

Start the MariaDB service and set it to start automatically when the server boots up.

systemctl start mariadb
systemctl enable mariadb

Step 2: Set MariaDB password on both Master and Slave

 

MariaDB/MySQL passwords are typically left blank by default, allowing unauthorised users to access the database. We must secure it by configuring a password and hardening it with a few additional settings. Run the following command on both the master and slave nodes to accomplish this.

mysql_secure_installation

Step 3: Configuring the Master node

Let’s configure the Master node now that we’ve hardened our MariaDB instances on both nodes.
First, we must allow MariaDB’s port 3306 to pass through the firewall. Run the following commands to accomplish this.
firewall-cmd --add-port=3306/tcp --zone=public --permanent

Reload the firewall to see the changes take effect.
firewall-cmd --relaod

Secondly, Make a few changes to the /etc/my.cnf file
vim /etc/my.cnf

We need to append the following lines in the [mysqld] section


[mysqld]
server_id=1
log-basename=master
log-bin
binlog-format=row
binlog-do-db=replica_db
[...]

Here, replica db refers to the database that will be created and replicated across the slave.
After that, use the following command to restart the MariaDB service:
systemctl restart mariadb

Now we are going to login to MariaDB as root user:
mysql -u root -p

Create the replica db database as the next step.
MariaDB [(none)]> CREATE DATABASE replica_db;

Create a Slave user with a password. We’ll use slave_user as the Slave username and P@ssW@Rd1890 as the password, for example:

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@ssW@Rd1890’;
Query OK, 0 rows affected (0.00 sec)

Then, as shown, flush the privileges.

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

MariaDB [(none)]> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)

Then, to see the master status, run the command below.
SHOW MASTER STATUS;

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

Then, to back up all of the Master databases, run the command below.
mysqldump --all-databases --user=root --password --master-data > masterdatabase.sql

In the current working directory, it will create a file called masterdatabase.sql .
Again We need to 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
Bye

The masterdatabase.sql file should now be copied to the slave server.
So the command will be:
scp masterdatabase.sql root@173.82.94.57:/home

Please keep in mind that our MariaDB slave server is located at 173.82.94.57.

Step 4: Configuring MariaDB Slave

It’s now time to set up the MariaDB Slave node.

Edit the /etc/my.cnf file
vim /etc/my.cnf

We need to append the following entries under the [mysqld] section as shown


[mysqld]
server-id = 2
replicate-do-db=replica_db
[...]

The database created on the Master Server node is replica db. Also, keep in mind that the master and slave servers should have different server-ids. The server-id is 2 in this case.

Save. Then we can exit the file.

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

Remember that the masterdatabase.sql file from the master server had already been copied to the slave server’s /home/ directory.

In order to apply the changes, restart the MariaDB service.
systemctl restart mariadb

Now log in as the root user to MariaDB.
mysql -u root -p

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

MariaDB [(none)]> STOP SLAVE;

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=’173.82.2.236′, MASTER_USER=’slave_user’, MASTER_PASSWORD=’P@ssW@Rd1890′, 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)

Then, to see the slave’s status, run the command below.
MariaDB [(none)]> SHOW SLAVE STATUS\G;

[Looking for a solution to another query? We are just a click away.]

Conclusion

To sum up, Bobcares’ expert engineers demonstrated how to handle MariaDB Master-Slave replication.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF