Monday, February 25, 2019

How to create a MongoDB Replica Set on Unix

Lets say, we want to create a 3 node mongo replication set on 3 machines -mongo_host1 ,mongo_host2, mongo_host3 with following log and data directory

Host1: mongo_host1
Data directory  : /u02/msetup/repl_data/mem1
Log directory : /u02/msetup/repl_data/log/mongod1.log
port:3000

Host2 : mongo_host2
Data directory  : /u02/msetup/repl_data/mem2
Log directory : /u02/msetup/repl_data/log/mongod2.log
port:3000

Host3 : mongo_host3
Data directory  : /u02/msetup/repl_data/mem3
Log directory : /u02/msetup/repl_data/log/mongod3.log
port:3000

1 Firstly,  we have to setup respective directories -
mkdir -p  /u02/msetup/repl_data/log
mkdir p  /u02/msetup/repl_data/mem1(for node 1)

2 Next, we create configuration file for mongo for mongo_host1
more /u02/msetup/repl_data/mem1/mongo1.conf
mongo1.conf

processManagement:
   fork: true
systemLog:
  destination: file
  logAppend: true
  path: /u02/msetup/repl_data/log/mongod1.log
# Where and how to store data.
storage:
  dbPath: /u02/msetup/repl_data/mem1
# network interfaces
net:
  port: 30000
  bindIp: 0.0.0.0
##  bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.
replication:
  replSetName: repl_data

3 Next we create similar files for other 2 nodes-mongo_host2 and mongo_host3
edit the respective conf file for log and data directory location

4 Next, we start each of these 3 mongo members as stand alone mongo

On mongo_host1 ->mongod --config /u02/msetup/repl_data/mem1/mongo1.conf
On mongo_host2 ->mongod --config /u02/msetup/repl_data/mem2/mongo2.conf
On mongo_host3 ->mongod --config /u02/msetup/repl_data/mem3/mongo3.conf


5 Now, we connect to mongo and define the configuration for replica set and initiate it
On mongo_host1,
mongo  --port 30000

cfg ={ _id : "repl_data", members: [ { _id:0 ,host:"mongo_host1:30000" }, { _id:1 ,host:"mongo_host2:30000" }, { _id:2 ,host:"mongo_host3:30000" } ] }

rs.initiate ( cfg )

rs.status() -> this will show information about all 3 members of replica set.

NOTE: To connect to secondary nodes, we have to issue rs.slaveOk(). Like in this case, to connect to mongo on mongo_host2 and mongo_host3-
mongo  --port 30000
rs.slaveOk()


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


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

Replica Set in MongoDB


  • Replica Set  in MongoDB ensure high availability and redundancy.
  • Replica Set is  basically set of different  mongod processes which can run on different machines or on same machine. Typically in a prod environment, these should run on different machines/nodes.
  • All members of replica set contains same data.
  • There can be only 1  read-write member which is called primary member.
  • All others members are read only members called secondary members.
  • Data is replicated from read-write member to read only members asynchronously.
  • Optionally, there can be arbiter which does not require dedicated hardware. Arbiter is not data bearing node. It just participates is elections to obtain majority.
  • It is recommended to have add number of nodes in Mongo replica set. We typically add a arbiter to make odd number of nodes in replica set.
  • When primary node fails, or does not communicate with other nodes in replica set( for by default 10 sec), an election happens. The purpose of elections is to choose a primary in mongo replica set, so that normal operations can be resumed (write can also happen now). Election can also happen in certain other circumstances.