Friday, April 20, 2018

Basic Replication Setup - MongoDB

Basic Replication Setup
  • Here am gonna tell you how to setup a replica set with 3 servers.
  • And in mongodb replication, the replication factor should be an odd no (i.e. the no of servers should be like 3, 5, 7 etc)
  • Here all the server are data baring nodes with no arbiters.
  • Also assuming that you have already installed mongodb in your machines
Servers:


ec2-34-230-32-183.compute-1.amazonaws.com
ec2-54-227-83-137.compute-1.amazonaws.com
ec2-52-90-220-63.compute-1.amazonaws.com

Sample Config File:

net:
  port: 27019
processManagement:
  fork: true
replication:
  replSetName: learning
storage:
  dbPath: /root/data
systemLog:
  destination: file
  logAppend: true
  logRotate: rename
  path: /root/mongod.log

  • copy the config file to all the 3 servers and create necessary directories if required.
Start each member of the replica set with the appropriate options.

From the normal standalone config file settings, we just need to add the below parameters, 

replication:
   replSetName: learning



Start all the 3 servers using the below command,

<bin_path> -f <config_file_path>
/usr/bin/mongod –f /root/mongod.conf



Connect a mongo shell to one of the mongod instances.

ec2-34-230-32-183.compute-1.amazonaws.com
mongo --port 27019




Initiate the replica set.

> rs.initiate()



Add the Replica Set Members

rs.add(“ec2-54-227-83-137.compute-1.amazonaws.com:27019”)
rs.add(“ec2-52-90-220-63.compute-1.amazonaws.com:27019”)



View the replica set configuration.

rs.conf()

learning:PRIMARY> rs.conf()
{
        "_id" : "learning",
        "version" : 3,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "ip-172-31-29-148.ec2.internal:27019",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "ec2-54-227-83-137.compute-1.amazonaws.com:27019",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "ec2-52-90-220-63.compute-1.amazonaws.com:27019",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

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

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

Ensure that the replica set has a primary.

rs.status()

learning:PRIMARY> rs.status()
{
        "set" : "learning",
        "date" : ISODate("2018-04-20T10:14:01.932Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "members" : [
                {
                        "_id" : 0,
                        "name" : "ip-172-31-29-148.ec2.internal:27019",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 253,
                        "optime" : {
                                "ts" : Timestamp(1524219067, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-04-20T10:11:07Z"),
                        "electionTime" : Timestamp(1524219023, 2),
                        "electionDate" : ISODate("2018-04-20T10:10:23Z"),
                        "configVersion" : 3,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "ec2-54-227-83-137.compute-1.amazonaws.com:27019",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 195,
                        "optime" : {
                                "ts" : Timestamp(1524219067, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-04-20T10:11:07Z"),
                        "lastHeartbeat" : ISODate("2018-04-20T10:14:01.487Z"),
                        "lastHeartbeatRecv" : ISODate("2018-04-20T10:14:00.466Z"),
                        "pingMs" : NumberLong(1),
                        "syncingTo" : "ip-172-31-29-148.ec2.internal:27019",
                        "configVersion" : 3
                },
                {
                        "_id" : 2,
                        "name" : "ec2-52-90-220-63.compute-1.amazonaws.com:27019",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 174,
                        "optime" : {
                                "ts" : Timestamp(1524219067, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-04-20T10:11:07Z"),
                        "lastHeartbeat" : ISODate("2018-04-20T10:14:01.479Z"),
                        "lastHeartbeatRecv" : ISODate("2018-04-20T10:13:57.478Z"),
                        "pingMs" : NumberLong(0),
                        "configVersion" : 3
                }
        ],
        "ok" : 1
}
learning:PRIMARY>

Another Method:
  • There is other way to initiate the replica set.
  • Instead of adding the servers one by one, you can add all at once.
Login to the server

ec2-34-230-32-183.compute-1.amazonaws.com
# mongo --port 27019

rs.initiate( {
   _id : "learning",
   members: [
      { _id: 0, host: " ec2-34-230-32-183.compute-1.amazonaws.com:27019" },
      { _id: 1, host: " ec2-54-227-83-137.compute-1.amazonaws.com:27019" },
      { _id: 2, host: " ec2-52-90-220-63.compute-1.amazonaws.com:27019" }
   ]
})



Some replica set commands

rs.help() -> will show all the commands which is related to replica set
rs.status() -> checks the replica set status

rs.initiate() -> initiates set with default settings
rs.conf() ->  get the current configuration object from local.system.replset
rs.reconfig( cfg ) -> updates the configuration of the running replica set with cfg
rs.add("hostname:port_no") -> to add a new member in the replica set
rs.remove("hostname:port_no") -> to remove a member from the replica set
rs.slaveOk() -> to read from secondary, should run this command in secondary node
rs.isMaster() -> to check who is primary

Done…

No comments:

Post a Comment