Sunday, 16 July 2017

Replication In MongoDB


Is your company facing problem of data storage and it's replication because of increasing data ?

We're here with an open source database system , MongoDB. 
I don't believe in writing long posts.So let's quickly start learning how replication can be achieved using mongodb.

Reqirements:  


                       Mongodb
                       Windows System

Download and install mongodb
                
          First create 2 folders in inside mongodb folder namely : data and log(It will create                     all log files) .Inside data create one more folder db (it will store all data).        
  •    Follow this link to download mongodb in your system  download 
  •    Installing mongodb in your system 

  1.              Open your installer file .
  2.              Custom Setup 
  3.              Change location by browsing (lookin mongodb)
  4.              Select ok 
  5.              Your installation will start 

              

BASIC TERMINOLOGIES IN REPLICATION


Before deploying replication set , you should make yourself familiar with following terminologies

Mongod:  core database process

Mongoscontroller and query router
Mongo : interactive MongoDB shell
Replication set : group of mongod instances

Oplog(Operation Log) : keeps rolling record of all that maintain the same dataset operations that                                                 modify data stored in your Database.


          Getting Started



Our first step is to create different directories which will act as different storage places for our streaming data.
         >E:\mkdir \data\rs1 \data\rs2 \data\rs3

*Starting three different mongodb instances(All below 3 commands must be executed in different cmd shell)

        >E:\mongodb\bin\mongod --replSet hello --logpath \mongodb\data\rs1\1.log 
          --dbpath   \mongodb\data\rs1 --port  27017 --smallfiles --oplogSize
       > E:\mongodb\bin\mongod --replSet hello --logpath \mongodb\data\rs1\2.log                   --dbpath    \mongodb\data\rs2 --port  27018 --smallfiles --oplogSize
       >E:\mongodb\bin\mongod --replSet hello --logpath \mongodb\data\rs1\3.log 
         --dbpath \mongodb\data\rs3 --port  27019 --smallfiles --oplogSize

*Interconnecting all 3 nodes

>mongo --port 27017
>config={_id:"hello",members:[{_id:0,host:"localhost: 27017 "},{_id:1,host:"localhost: 27018"},{_id:2,host:"localhost: 27019"}]};
>rs.initiate(config);
>rs.status()            //To check status of replica sets

*Testing

Insert document into primary node and after that read the inserted document

db.assignments.insert({name:"up",duedate:"19th january"});
db.assignments.find().pretty()

*Connecting secondary node & read data inserted by primary node

note:  By default ,you can't read data from secondary node. Hence you will get an error while trying to read data from node.Hence us command rs.slaveOk()

>mongo --port 27018
>rs.slaveOk();
>db.assignments.find();

note:  We can't insert values from secondary node.

*Checking how replication is happening internally

To do so you need to analyze the oplog.rs file in each nodes including primary & secondary
>mongo --port 27107
hello:PRIMARY>use local
hello:PRIMARY>show collections
hello:PRIMARY>db.oplog.rs.find().pretty()

*Failure in replication

primary node
>use admin
>db.shutdownServer()

After this you will see that automatically ,out of remaining 2 nodes ,one will be assigned as primary node. When the node(primary) ,whose server we have shuted down is again added in the cluster then it will be added as secondary node but not primary.

To again start the previous node
>mongo --port 27017
>rs.slaveOk();
>db.assignments.find();

Adavantages of replication in MongoDB




2) Auto-recovery of  replication failure





No comments:

Post a Comment