Showing posts with label secondary. Show all posts
Showing posts with label secondary. Show all posts

Sunday, February 24, 2019

How to create MongoDB replica set on Windows

In this tutorial, we will discuss about how to create a mongodb replica set on windows-

1 First start 3 mongod processes individually-
mongod --port 30001  --dbpath C:\app\mongo\mongo_replica\mongoag1 --logpath C:\app\mongo\mongo_replica\mongoag1\log1  --replSet rs1 --logappend --bind_ip 127.0.0.1

mongod --port 30002  --dbpath C:\app\mongo\mongo_replica\mongoag2 --logpath C:\app\mongo\mongo_replica\mongoag2\log2  --replSet rs1 --logappend --bind_ip 127.0.0.1

mongod --port 30003  --dbpath C:\app\mongo\mongo_replica\mongoag3 --logpath C:\app\mongo\mongo_replica\mongoag3\log3  --replSet rs1 --logappend --bind_ip 127.0.0.1

2 Now login to each member  individually to check that mongo is up and running for all 3 members

mongo --port 30001
mongo --port 30002
mongo --port 30003

3 You may disconnect from all 3 and connect to one member which you want to make primary
mongo --port 30001

Issue the following commands to define the configuration of replica set and initiate it. Here replica set name is "rs1".

cfg= { _id : "rs1",   members: [
      { _id: 0, host: "127.0.0.1:30001" },
      { _id: 1, host: "127.0.0.1:30002" },
      { _id: 2, host: "127.0.0.1:30003" }
   ] }

rs.initiate (cfg)

The prompt will automatically change  to "OTHER", then to "SECONDARY" and finally to "PRIMARY"

4 Now issue the following command to validate the configuration of replica set -

rs1:PRIMARY> rs.status()
{
        "set" : "rs1",
        "date" : ISODate("2019-02-24T12:03:20.849Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1551009787, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1551009787, 1),
                        "t" : NumberLong(1)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1551009787, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1551009787, 1),
                        "t" : NumberLong(1)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "127.0.0.1:30001",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 85,
                        "optime" : {
                                "ts" : Timestamp(1551009787, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2019-02-24T12:03:07Z"),
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1551009785, 1),
                        "electionDate" : ISODate("2019-02-24T12:03:05Z"),
                        "configVersion" : 1,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "127.0.0.1:30002",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 26,
                        "optime" : {
                                "ts" : Timestamp(1551009787, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1551009787, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2019-02-24T12:03:07Z"),
                        "optimeDurableDate" : ISODate("2019-02-24T12:03:07Z"),
                        "lastHeartbeat" : ISODate("2019-02-24T12:03:19.912Z"),
                        "lastHeartbeatRecv" : ISODate("2019-02-24T12:03:19.153Z"),
                        "pingMs" : NumberLong(1),
                        "syncingTo" : "127.0.0.1:30001",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "127.0.0.1:30003",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 26,
                        "optime" : {
                                "ts" : Timestamp(1551009787, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1551009787, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2019-02-24T12:03:07Z"),
                        "optimeDurableDate" : ISODate("2019-02-24T12:03:07Z"),
                        "lastHeartbeat" : ISODate("2019-02-24T12:03:19.915Z"),
                        "lastHeartbeatRecv" : ISODate("2019-02-24T12:03:19.635Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "127.0.0.1:30001",
                        "configVersion" : 1
                }
        ],
        "ok" : 1,
        "operationTime" : Timestamp(1551009787, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1551009787, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
rs1:PRIMARY>

NOTE: To connect to secondary nodes, we have to issue rs.slaveOk(). Like in this case, to connect to mongo on secondary nodes-
mongo  --port 30001 (or port 30002)
rs.slaveOk()

If you want to set up mongo replication on unix, then plz go through following link for creating mongo replica set-
http://www.dbatraininghub.com/2019/02/how-to-create-replica-set-in-unix.html