Home Mongo DB Solution The solution to Homework 6.5 M101JS: MongoDB for Node.js Developers

The solution to Homework 6.5 M101JS: MongoDB for Node.js Developers

Homework: Homework 6.5

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. backslashes “\”) 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.

Now install and run validate.js to confirm that it works.

npm install
node validate.js

Validate connects to your local replica set and checks that it has three nodes. Type the validation code below.

Solution: XdYfY6aqjqS3ik35qS6v   My validation code is above, but I hope it may differ for you so be careful before submitting.   How i achieve that validation code? These are my own steps so may differ for you:

  • Gone to C: drive data/ folder and created rs1/, rs2 and rs3 folders
  • Open command prompt and path to C:/data/rs1 and run following command: mongod –replSet m101 –logpath “1.log” –dbpath C:/data/rs1 –port 27017 –smallfiles –oplogSize 64
  • Then opened another command prompt and path to C:/data/rs2 and fun following command: mongod –replSet m101 –logpath “2.log” –dbpath C:/data/rs2 –port 27018 –smallfiles –oplogSize 64
  • Again opened another command prompt and path to C:/data/rs3 and run following command: mongod –replSet m101 –logpath “3.log” –dbpath C:/data/rs3 –port 27019 –smallfiles –oplogSize 64
  • Again open another command prompt and run following command: mongo –port 27017
  • Then mongo database shell get opened on which run following command: config = { _id: “m101”, members:[ { _id : 0, host : “localhost:27017”}, { _id : 1, host : “localhost:27018”}, { _id : 2, host : “localhost:27019”} ] }; rs.initiate(config); You will see following message if other database ports are not opened:
    {“ok” : 0, “errmsg” : “server is not running with –replSet” }
    If all ports databases are running then you will get following success message
    {
    “info” : “Config now saved locally. Should come online in about a minute.”,
    “ok” : 1
    }
  • Now check status of the replicated databases with following command: > rs.status() { “set” : “m101”, “date” : ISODate(“2015-11-22T20:24:21Z”), “myState” : 1, “members” : [ { “_id” : 0, “name” : “localhost:27017”, “health” : 1, “state” : 1, “stateStr” : “PRIMARY”, “uptime” : 225, “optime” : Timestamp(1448223786, 1), “optimeDate” : ISODate(“2015-11-22T20:23:06Z”), “electionTime” : Timestamp(1448223795, 1), “electionDate” : ISODate(“2015-11-22T20:23:15Z”), “self” : true }, { “_id” : 1, “name” : “localhost:27018”, “health” : 1, “state” : 2, “stateStr” : “SECONDARY”, “uptime” : 74, “optime” : Timestamp(1448223786, 1), “optimeDate” : ISODate(“2015-11-22T20:23:06Z”), “lastHeartbeat” : ISODate(“2015-11-22T20:24:19Z”), “lastHeartbeatRecv” : ISODate(“2015-11-22T20:24:19Z”), “pingMs” : 0, “syncingTo” : “localhost:27017” }, { “_id” : 2, “name” : “localhost:27019”, “health” : 1, “state” : 2, “stateStr” : “SECONDARY”, “uptime” : 74, “optime” : Timestamp(1448223786, 1), “optimeDate” : ISODate(“2015-11-22T20:23:06Z”), “lastHeartbeat” : ISODate(“2015-11-22T20:24:19Z”), “lastHeartbeatRecv” : ISODate(“2015-11-22T20:24:20Z”), “pingMs” : 0, “syncingTo” : “localhost:27017” } ], “ok” : 1 } m101:PRIMARY> It shows all are OK
  • Now download the handout provided for the assignment, then unzip it.
  • Run another command prompt again and go to the path where you unzip your assignment and to hw6-5/ folder and run the following command: npm install node validate.js
  • You will see validation code as following in command line:

This way I have achieved my validation code, I hope it will be the same for you.
Please let me know if there is any way that I can run multiple command prompt instead of Opening multiple open.
Likewise, let me know if there is any simpler way.

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exit mobile version