Monday, August 26, 2019

MongoDB: All about MongoDB Replica Set

Replica set basic architecture. PRIMARY - write can happen only on primary and read can happen only on SECONDARIES

OPLOG are the files which replica set secondaries tail (like unix tail command) and read all the writes happened on PRIMARY sequentially and then run the same write queries to be in sync with PRIMARY.

Lets start with basic replicaset

mkdir /home/mongod/rs1
[mongod@sikki4u1c rs1]$  mongod --dbpath /home/mongod/rs1 --replSet r1 --logpath /var/log/mongodb/mongod_rs1.log --fork
about to fork child process, waiting until server is ready for connections.
forked process: 3286
child process started successfully, parent exiting
[mongod@sikki4u1c rs1]$

If you see the log

"2019-08-25T13:31:12.626+0000 I REPL     [initandlisten] Did not find local replica set configuration document at startup;  NoMatchingDocument: Did not find replica set configuration document in local.system.replset"

Now,we need to initiate replcaset, using rs.initiate() function.

mongo

> rs.initiate()
{
        "info2" : "no configuration specified. Using a default configuration for the set",
        "me" : "localhost:27017",
        "ok" : 1,
        "operationTime" : Timestamp(1566740045, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1566740045, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

r1:SECONDARY>  -----press enter here
r1:PRIMARY>    -----there you go, this instance is PRIMARY now.

To see the configuation mongo created for us, you can run rs.config / rs.conf

r1:PRIMARY> rs.config()
{
        "_id" : "r1",
        "version" : 1,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5d628e4dfb9311e2065bfcee")
        }
}
r1:PRIMARY>

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

To check the status of replica set

rs.status()

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home