Sunday, February 12, 2017

Replication in MongoDB

Introduction

-> Replication provides redundancy and increases data availability.
-> With multiple copies of data on different database servers, replication provides a level of fault tolerance against the loss of a single database server
-> A replica set is a group of mongod instances that maintain the same data set
-> A replica set contains several data bearing nodes and optionally one arbiter node.
-> Of the data bearing nodes, one and only one member is deemed the primary node, while the other nodes are deemed secondary nodes.

Replication

-> Durability
-> Availability
-> Redundant copies across multiple machines.
-> We can say - replicas, copies or backups
-> 'HA' - high availability ( failover )
-> Data Safety ( Durability )
-> extra copies
-> 'DR' - Disaster recovery

'Asynchronous' replication


            

Replica Sets

-> Master - slave replication
-> Can be one primary and multiple secondary’s
-> Or Primary, secondary and an arbiter


                         

Automatic failover

-> During system crash, data will be archived and restored as soon as the system comes back.


                          

Driver - Will reach out to all the members of the set and see who is primary.

Replication Options

--replSet -> replica set name
--logappend -> to overwrite the logs, while start or restart the server
--port -> port no
--dbpath -> path to store datas
--oplogSize -> to specify certain size for oplog in MB, by default - 5% of your available free space on disk
--smallfiles -> just to keep the datafiles little bit smaller and its not advised to use this option in production
--fork -> to run the process background

Starting Replica sets

-> ports
-> dbpath
-> Replica set name



ex

mongod --port 27001 --replSet abc --dbpath /data/db --logpath /data/log --logappend --oplogSize 50 --smallfiles --fork

Initiating the replica set

>rs.initiate() - to initiate a replica set

ex

$mongo --port 27001

>rs.initiate()

>rs.status() - to check the status of the replica set

Adding members to the replica set

>rs.add("hostname:port_no")

ex

>rs.add("localhost:27002")

>rs.add("localhost:27003")

abc:primary > rs.status() - to check the status of the replica set


Some replica set commands

rs.help() -> will show all the commands which is realted 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












No comments:

Post a Comment