Table of Contents

Overview

Replication set according mongo is set of servers who shared the same data. In nutshell, like any other database, mongo support replication. The Replica Sets are the latest evolution of this approach. In ReplicaSet configuration, the primary database is consistently recovering the other “Secondary servers”

In our settings we have the following configuration:

In order to set mongo database, we have to install mongo's binaries on at least 2 servers. Detailed instructions you can find below:

After mongo is installed there are couple things which has to be done:

Start the mongod

Before starting a replica set, we have to start mongo in a special way:

[root@lparamongo mongo]# mongod -f /etc/mongod.conf -fork --replSet testset
                                       ^              ^       ^       ^
                                       |              |       |       |
                               Start using file   with child As RS  Name of the Replication Set(RS)

about to fork child process, waiting until server is ready for connections.
forked process: 2410
child process started successfully, parent exiting
[root@lparamongo mongo]# mongo --port 9005
MongoDB shell version: 2.6.12
connecting to: 127.0.0.1:9005/test
>

Issue that command on all of the servers:

Second Node

[root@lparbmongo mongo]#  mongod -f /etc/mongod.conf -fork --replSet testset
about to fork child process, waiting until server is ready for connections.
forked process: 2949
child process started successfully, parent exiting
[root@lparbmongo mongo]#

Third Node

[root@lparcmongo ~]# mongod -f /etc/mongod.conf -fork --replSet testset
about to fork child process, waiting until server is ready for connections.
forked process: 3338
child process started successfully, parent exiting
[root@lparcmongo ~]#

Initialize the ReplicaSet

The initialization is done ALWAYS from the PRIMARY node ( the one who will be available) as follows:

> rs.initiate()
{
        "info2" : "no configuration explicitly specified -- making one",
        "me" : "lparamongo:9005",
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}
> rs.status()
{
        "set" : "testset",
        "date" : ISODate("2018-03-24T21:32:36Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "lparamongo:9005",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 71,
                        "optime" : Timestamp(1521927136, 1),
                        "optimeDate" : ISODate("2018-03-24T21:32:16Z"),
                        "electionTime" : Timestamp(1521927136, 2),
                        "electionDate" : ISODate("2018-03-24T21:32:16Z"),
                        "self" : true
                }
        ],
        "ok" : 1
}
testset:PRIMARY>

This script will initialize the replica set.

P.S. Do not run this command on the other nodes or you will receive FATAL state when you try to add the members:

testset:FATAL> rs.status()
{
        "set" : "testset",
        "date" : ISODate("2018-03-24T21:26:02Z"),
        "myState" : 4,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "lparamongo:9005",
                        "health" : 1,
                        "state" : 4,
                        **"stateStr" : "FATAL",**    <- Fatal due to double initialization of the replica set
                        "uptime" : 243,
                        "optime" : Timestamp(1521925807, 1),
                        "optimeDate" : ISODate("2018-03-24T21:10:07Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "lparbmongo:9005",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 243,
                        "optime" : Timestamp(1521925457, 1),
                        "optimeDate" : ISODate("2018-03-24T21:04:17Z"),
                        "lastHeartbeat" : ISODate("2018-03-24T21:26:01Z"),
                        "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
                        "pingMs" : 0,
                        "electionTime" : Timestamp(1521926475, 11),
                        "electionDate" : ISODate("2018-03-24T21:21:15Z")
                }
        ],
        "ok" : 1
}

Add the other members

Members are ALWAYS added from the PRIMARY, in our case: lparamongo, as follows:

testset:PRIMARY> use admin
switched to db admin
testset:PRIMARY> rs.add("lparbmongo:9005")
{ "ok" : 1 }
testset:PRIMARY> rs.status()
{
        "set" : "testset",
        "date" : ISODate("2018-03-24T21:35:47Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "lparamongo:9005",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 262,
                        "optime" : Timestamp(1521927343, 1),
                        "optimeDate" : ISODate("2018-03-24T21:35:43Z"),
                        "electionTime" : Timestamp(1521927136, 2),
                        "electionDate" : ISODate("2018-03-24T21:32:16Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "lparbmongo:9005",
                        "health" : 1,
                        "state" : 5,
                        "stateStr" : "STARTUP2",
                        "uptime" : 4,
                        "optime" : Timestamp(0, 0),
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2018-03-24T21:35:45Z"),
                        "lastHeartbeatRecv" : ISODate("2018-03-24T21:35:46Z"),
                        "pingMs" : 0,
                        "lastHeartbeatMessage" : "initial sync need a member to be primary or secondary to do our initial sync"
                }
        ],
        "ok" : 1
}

The 2nd server will appear as starting for couple seconds until the primary and secondary settle themselves, the final output you can see below, after I have added the 3rd node the same way I added the 2nd:

testset:PRIMARY> rs.status()
{
        "set" : "testset",
        "date" : ISODate("2018-03-24T22:06:36Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "lparamongo:9005",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 2111,
                        "optime" : Timestamp(1521929109, 1),
                        "optimeDate" : ISODate("2018-03-24T22:05:09Z"),
                        "electionTime" : Timestamp(1521927136, 2),
                        "electionDate" : ISODate("2018-03-24T21:32:16Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "lparbmongo:9005",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 1853,
                        "optime" : Timestamp(1521929109, 1),
                        "optimeDate" : ISODate("2018-03-24T22:05:09Z"),
                        "lastHeartbeat" : ISODate("2018-03-24T22:06:36Z"),
                        "lastHeartbeatRecv" : ISODate("2018-03-24T22:06:35Z"),
                        "pingMs" : 0,
                        "syncingTo" : "lparamongo:9005"
                },
                {
                        "_id" : 2,
                        "name" : "lparcmongo:9005",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 87,
                        "optime" : Timestamp(1521929109, 1),
                        "optimeDate" : ISODate("2018-03-24T22:05:09Z"),
                        "lastHeartbeat" : ISODate("2018-03-24T22:06:35Z"),
                        "lastHeartbeatRecv" : ISODate("2018-03-24T22:06:36Z"),
                        "pingMs" : 0,
                        "syncingTo" : "lparamongo:9005"
                }
        ],
        "ok" : 1
}