Tuesday, February 17, 2015

Creating a mongo DB replica set in windows desktop

I was going through mongo DB online course “M101JS: MONGODB FOR NODE.JS DEVELOPERS” and got stuck on the assignment “HOMEWORK: HOMEWORK 6.5”. As I was working on windows and the steps given was for MAC or Linux environment. I thought of sharing this with others. The original steps are as follows-

In this homework you will build a small replica set on your own computer. We will check that it works with validate.js, which you should download from the Download Handout link. Create three directories for the three mongod processes.

On unix or mac, this could be done as follows:
mkdir -p /data/rs1 /data/rs2 /data/rs3
Now start three mongo instances as follows. Note that are three commands. The browser is probably wrapping them visually.
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
Windows users: Omit -p from mkdir. Also omit --fork and use start mongod with Windows compatible paths (i.e. back slashes "\") for the --dbpath argument (e.g; C:\data\rs1).

Now connect to a mongo shell and make sure it comes up
mongo --port 27017
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.

The steps I have followed as below-

I kept all the database file in the following folder - D:/Anup/POCs/MongoDB/data/test_replicaset. So the create directory looks like below-
mkdir "D:/Anup/POCs/MongoDB/data/test_replicaset/rs1" "D:/Anup/POCs/MongoDB/data/test_replicaset/rs2" "D:/Anup/POCs/MongoDB/data/test_replicaset/rs3"


Now before moving further please make sure that in command prompt you are already in the mongo DB bin folder. In my case the mongo.exe and mongod.exe are in the folder -D:\Anup\POCs\MongoDB.



Now as per the given instruction the mnogo DB command for create replica set for windows looks like below-
mongod --replSet m101 --logpath "D:/Anup/POCs/MongoDB/data/test_replicaset/1.log" --dbpath D:/Anup/POCs/MongoDB/data/test_replicaset/rs1 --port 27017 --smallfiles --oplogSize 64 start mongod  
mongod --replSet m101 --logpath "D:/Anup/POCs/MongoDB/data/test_replicaset/2.log" --dbpath D:/Anup/POCs/MongoDB/data/test_replicaset/rs2 --port 27018 --smallfiles --oplogSize 64 start mongod 
mongod --replSet m101 --logpath "D:/Anup/POCs/MongoDB/data/test_replicaset/3.log" --dbpath D:/Anup/POCs/MongoDB/data/test_replicaset/rs3 --port 27019 --smallfiles --oplogSize 64 start mongod
But for some reason I was unable to execute the script in one go. So what I have done is opened three command prompts and executed the command separately like below ( removing start mongod at the end).



Next I have opened another command prompt to run mongo command-
mongo --port 27017
Once the mongo shell connected to test database, executed the remaining of the config command and checked the status. It’s started running.