Wednesday 27 June 2018

Mongo ReplicaSet

Hi Guys this is my first post in Mongodb and today i give you a small demonstration to configure replica set.

  • But first thing is know about the replica set? 
  • what is replicaset? 
  • How they work ? go through with google all this type of questions.


Now we are going to configure replicaset.

=============
My Envirenment
=============

3 Servers -> 1 Primary -> 2 Secondary
OS Useing RHEL7
MongoDB version 3.4

=========
# Host File
=========

- Entry in /etc/hosts in all members

[root@db1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.219.130         mongodb1.com    db1
192.168.219.140         mongodb2.com    db2
192.168.219.141         mongodb3.com    db3

==============
# Disable Selinux
==============

Check the SELinux status with the command.
[root@db1 ~]# getenforce
enabled

[root@db1 ~]# cat /etc/selinux/config
SELINUX=disabled

[root@db1 ~]# getenforce
disabled

==================
# Configure Firewalld
==================
In the first step, we already disabled SELinux. For security reasons, we will now enable firewalld on all nodes and open only the ports that are used by MongoDB and SSH.

- Install Firewalld with the yum command.

[root@db1 ~]# yum -y install firewalld

- Start firewalld and enable it to start at boot time.

[root@db1 ~]# systemctl start firewalld
[root@db1 ~]# systemctl enable firewalld

- Next, open your ssh port and the MongoDB default port 27017.

[root@db1 ~]# firewall-cmd --permanent --add-port=22/tcp
[root@db1 ~]# firewall-cmd --permanent --add-port=27017/tcp

- Reload firewalld to apply the changes.

[root@db1 ~]# firewall-cmd --reload



=====================
# Mongo Configuration File
=====================

- Below the configuration file parameter which is same in all the members

/etc/mongod.conf
# network interfaces
net:
  port: 27017
  #bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.

replication:
  replSetName: rs0


=========================
# MongoDB Replica Set initiate
=========================
In this step, we will create the replica set. We will use the 'mongo1' server as 'PRIMARY' node, and 'db2' and 'db3' as 'SECONDARY' nodes.

Initiate the replica set from the mongo1 server with the query below.

[root@db1 ~]# rs.initiate()
Make sure 'ok' value is 1.

-Now add the 'db2' and 'db3' nodes to the replica sets.

[root@db1 ~]# rs.add("db2:27017")
[root@db1 ~]# rs.add("db3:27017")

You will see the results below and make sure there is no error.


- Next, check the replica sets status with the rs query below.

[root@db1 ~]# rs.status()

- Another query to check the status is:

[root@db1 ~]# rs.isMaster()



==================
# Test the Replication
==================

Test the data set replication from the 'PRIMARY' instance 'mongo1' to 'SECONDARY' nodes 'db2' and 'db3'.

In this step, we will try to write or create a new database on the 'PRIMARY' node 'db1',
then check if the replication is working by checking the database on 'SECONDARY' nodes 'db2' and 'db3'.

- Login to the 'db1' server and open mongo shell.

[root@db1 ~]# mongo

- Now create a new database 'lemp' and new 'stack' collection for the database.

> use lemp
> db.stack.save(
{
    "desc": "LEMP Stack",
    "apps":  ["Linux", "Nginx", "MySQL", "PHP"],
})




- Next, go to the 'SECONDARY' node 'db2' and open the mongo shell.

[root@db1 ~]# mongo

Enable reading from the 'SECONDARY' node with the query 'rs.slaveOk()', and then check if the 'lemp' database exists on the 'SECONDARY' nodes.

> rs.slaveOk()
> show dbs
> use lemp
> show collections
> db.stack.find()


===================
# Check Replication Lag
===================

> rs.printReplicationinfo()
> rs.printSlaveReplicationinfo() # show all the members with replication lag.

rs0:PRIMARY> rs.printSlaveReplicationInfo();
source: db2:27017
        syncedTo: Mon Jan 15 2018 21:49:07 GMT+0530 (IST)
        0 secs (0 hrs) behind the primary
source: db3:27017
        syncedTo: Thu Jan 01 1970 05:30:00 GMT+0530 (IST)
        1516033147 secs (421120.32 hrs) behind the primary


rs0:PRIMARY> rs.printSlaveReplicationInfo();
source: db2:27017
        syncedTo: Mon Jan 15 2018 21:49:07 GMT+0530 (IST)
        0 secs (0 hrs) behind the primary
source: db3:27017
        syncedTo: Thu Jan 01 1970 05:30:00 GMT+0530 (IST)
        1516033147 secs (421120.32 hrs) behind the primary

rs0:PRIMARY> rs.status();
Test Demo: Down the db3 member instanace throught db.serverShutdown(); and execute above command and see the status.



No comments: