Saturday, November 17, 2018

MongoDB Replcation Setup

MongoDB Replica-Set Configuration
=========================================
we are configuring the Mongo replicaset on Linux server only for practice purpose

Primary+Seconday+Secondary

[mongodb@sainath data]$ pwd
/u02/mongo/data

Creating Directory's for the data path

[mongodb@sainath data]$ mkdir sai_r1
[mongodb@sainath data]$ mkdir sai_r2
[mongodb@sainath data]$ mkdir sai_r3

[mongodb@sainath data]$ ls -ltr

drwxrwxr-x. 2 mongodb mongodb 4096 Nov 16 22:20 sai_r1
drwxrwxr-x. 2 mongodb mongodb 4096 Nov 16 22:20 sai_r2
drwxrwxr-x. 2 mongodb mongodb 4096 Nov 16 22:20 sai_r3



ReplicaSet Name mentioned here is R

Port Numbers We have used are 27027 , 27028 , 27029

Startng Mongod service with the  below commands in different sessions
========================================================================
mongod --port 27027 --dbpath /u02/mongo/data/sai_r1 --replSet R
mongod --port 27028 --dbpath /u02/mongo/data/sai_r2 --replSet R
mongod --port 27029 --dbpath /u02/mongo/data/sai_r3 --replSet R

Connecting the mongo session

[mongodb@sainath data]$ mongo --port 27027
 MongoDB shell version v3.6.0-rc0
connecting to: mongodb://127.0.0.1:27027/
MongoDB server version: 3.6.0-rc0
Server has startup warnings:
2018-11-16T22:24:28.495+0530 I CONTROL  [initandlisten]

Initiate the replSet by using command rs.initiate()

> rs.initiate()
{
        "info2" : "no configuration specified. Using a default configuration for the set",
        "me" : "sainath.com:27027",
        "ok" : 0,
        "errmsg" : "No host described in new configuration 1 for replica set R maps to this node",
        "code" : 93,
        "codeName" : "InvalidReplicaSetConfig"
}


Configurinng adding all the mongod servers to cluster replSet R
===============================================================
> config = { "_id": "R",
... "members" : [
... { "_id" : 0, "host" : "localhost:27027" },
... { "_id" : 1, "host" : "localhost:27028" },
... { "_id" : 2, "host" : "localhost:27029" }
... ]}
{
        "_id" : "R",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:27027"
                },
                {
                        "_id" : 1,
                        "host" : "localhost:27028"
                },
                {
                        "_id" : 2,
                        "host" : "localhost:27029"
                }
        ]
}
> rs.initiate(config)
{
        "ok" : 1,
        "operationTime" : Timestamp(1542387435, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1542387435, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
R:PRIMARY>   rs.status()
{
        "set" : "R",
        "date" : ISODate("2018-11-16T16:57:37.161Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1542387448, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1542387448, 1),
                        "t" : NumberLong(1)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1542387448, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1542387448, 1),
                        "t" : NumberLong(1)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27027",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 190,
                        "optime" : {
                                "ts" : Timestamp(1542387448, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-11-16T16:57:28Z"),
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1542387446, 1),
                        "electionDate" : ISODate("2018-11-16T16:57:26Z"),
                        "configVersion" : 1,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "localhost:27028",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 21,
                        "optime" : {
                                "ts" : Timestamp(1542387448, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1542387448, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-11-16T16:57:28Z"),
                        "optimeDurableDate" : ISODate("2018-11-16T16:57:28Z"),
                        "lastHeartbeat" : ISODate("2018-11-16T16:57:36.115Z"),
                        "lastHeartbeatRecv" : ISODate("2018-11-16T16:57:36.526Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "localhost:27027",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "localhost:27029",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 21,
                        "optime" : {
                                "ts" : Timestamp(1542387448, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1542387448, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-11-16T16:57:28Z"),
                        "optimeDurableDate" : ISODate("2018-11-16T16:57:28Z"),
                        "lastHeartbeat" : ISODate("2018-11-16T16:57:36.116Z"),
                        "lastHeartbeatRecv" : ISODate("2018-11-16T16:57:36.528Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "localhost:27027",
                        "configVersion" : 1
                }
        ],
        "ok" : 1,
        "operationTime" : Timestamp(1542387448, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1542387448, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
R:PRIMARY> rs.config()
{
        "_id" : "R",
        "version" : 1,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:27027",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "localhost:27028",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "localhost:27029",
                        "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("5beef6eb7e1ec6c6b47764a4")
        }
}



Setting Priorities
======================
cfg=rs.conf()
cfg.members[0].priority=3
cfg.members[1].priority=4
cfg.members[2].priority=3
rs.reconfig(cfg)


R:PRIMARY> cfg=rs.conf()
{
        "_id" : "R",
        "version" : 1,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:27027",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "localhost:27028",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "localhost:27029",
                        "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("5beef6eb7e1ec6c6b47764a4")
        }
}
R:PRIMARY> cfg.members[0].priority=3
3
R:PRIMARY> cfg.members[1].priority=4
4
R:PRIMARY> cfg.members[2].priority=3
3
R:PRIMARY> rs.reconfig(cfg)
{
        "ok" : 1,
        "operationTime" : Timestamp(1542387990, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1542387990, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
R:PRIMARY>
> rs.status()
{
        "set" : "R",
        "date" : ISODate("2018-11-16T17:11:06.924Z"),
        "myState" : 1,
        "term" : NumberLong(3),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1542388261, 1),
                        "t" : NumberLong(3)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1542388261, 1),
                        "t" : NumberLong(3)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1542388261, 1),
                        "t" : NumberLong(3)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1542388261, 1),
                        "t" : NumberLong(3)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27027",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 999,
                        "optime" : {
                                "ts" : Timestamp(1542388261, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDate" : ISODate("2018-11-16T17:11:01Z"),
                        "electionTime" : Timestamp(1542388260, 1),
                        "electionDate" : ISODate("2018-11-16T17:11:00Z"),
                        "configVersion" : 4,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "localhost:27028",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 831,
                        "optime" : {
                                "ts" : Timestamp(1542388249, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1542388249, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2018-11-16T17:10:49Z"),
                        "optimeDurableDate" : ISODate("2018-11-16T17:10:49Z"),
                        "lastHeartbeat" : ISODate("2018-11-16T17:11:06.162Z"),
                        "lastHeartbeatRecv" : ISODate("2018-11-16T17:11:06.347Z"),
                        "pingMs" : NumberLong(0),
                        "configVersion" : 4
                },
                {
                        "_id" : 2,
                        "name" : "localhost:27029",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 831,
                        "optime" : {
                                "ts" : Timestamp(1542388261, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1542388261, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDate" : ISODate("2018-11-16T17:11:01Z"),
                        "optimeDurableDate" : ISODate("2018-11-16T17:11:01Z"),
                        "lastHeartbeat" : ISODate("2018-11-16T17:11:06.162Z"),
                        "lastHeartbeatRecv" : ISODate("2018-11-16T17:11:05.381Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "localhost:27027",
                        "configVersion" : 4
                }
        ],
        "ok" : 1,
        "operationTime" : Timestamp(1542388261, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1542388261, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

No comments:

Post a Comment