Saturday 21 February 2015

mongoDB : Creating a replica set


In this tutorial, I am going to explain, how to create a simple replica set in your computer.

Step 1: Create three directories for three mongo processes.
On Linux
mkdir -p /data/rs1 /data/rs2 /data/rs3

On Windows
mkdir \data\rs1 \data\rs2 \data\rs3

Step 2: Now start three mongo instances as follows.
On Linux
mongod --replSet m101 --logpath "1.log" --dbpath /data/rs1 --port 27017 --smallfiles --oplogSize 64 --fork
 
mongod --replSet m101 --logpath "2.log" --dbpath /data/rs2 --port 27018 --smallfiles --oplogSize 64 --fork

mongod --replSet m101 --logpath "3.log" --dbpath /data/rs3 --port 27019 --smallfiles --oplogSize 64 –fork


On Windows
start mongod --replSet m101 --logpath 1.log --dbpath \data\rs1 --port 27017 --smallfiles --oplogSize 64
start mongod --replSet m101 --logpath 2.log --dbpath \data\rs2 --port 27018 --smallfiles --oplogSize 64
start mongod --replSet m101 --logpath 3.log --dbpath \data\rs3 --port 27019 --smallfiles --oplogSize 64


Step 3: Now connect to a mongo shell and make sure it comes up.
mongo --port 27017

Step 4: Now you will create the replica set. Type the following commands into the mongo shell.

config = { _id: "m101", members:[
          { _id : 0, host : "localhost:27017"},
          { _id : 1, host : "localhost:27018"},
          { _id : 2, host : "localhost:27019"} ]
};
rs.initiate(config);


At this point, the replica set should be coming up. You can type

“rs.status()”to see the state of replication

> rs.status()
{
        "set" : "m101",
        "date" : ISODate("2015-02-13T13:04:15Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 174,
                        "optime" : Timestamp(1423832609, 1),
                        "optimeDate" : ISODate("2015-02-13T13:03:29Z"),
                        "electionTime" : Timestamp(1423832618, 1),
                        "electionDate" : ISODate("2015-02-13T13:03:38Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "localhost:27018",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 46,
                        "optime" : Timestamp(1423832609, 1),
                        "optimeDate" : ISODate("2015-02-13T13:03:29Z"),
                        "lastHeartbeat" : ISODate("2015-02-13T13:04:14Z"),
                        "lastHeartbeatRecv" : ISODate("2015-02-13T13:04:14Z"),
                        "pingMs" : 1,
                        "lastHeartbeatMessage" : "syncing to: localhost:27017",
                        "syncingTo" : "localhost:27017"
                },
                {
                        "_id" : 2,
                        "name" : "localhost:27019",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 46,
                        "optime" : Timestamp(1423832609, 1),
                        "optimeDate" : ISODate("2015-02-13T13:03:29Z"),
                        "lastHeartbeat" : ISODate("2015-02-13T13:04:14Z"),
                        "lastHeartbeatRecv" : ISODate("2015-02-13T13:04:14Z"),
                        "pingMs" : 0,
                        "lastHeartbeatMessage" : "syncing to: localhost:27017",
                        "syncingTo" : "localhost:27017"
                }
        ],
        "ok" : 1
}





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment