What is MongoDB Replication and How to Configure the Replication?

mongodb replication step by step

What is MongoDB Server Replication?

 

Replication is the method or process of synchronizing data across multiple servers. Replication provides redundancy and high availability. A replica set in MongoDB server is a group of mongod processes in multiple DB server that maintain the same data set, Multiple copies of data on different database servers.

The reason we use replica is that, Replication protects a database from the loss of a single server. It will keep our data safe on multiple servers so that hardware failure and service interruptions will not cause any data loss, because always there will be a working DB server.

In a replica, one node is the primary node that receives all write operations All other nodes such as secondaries which is an instance in replica apply the same operations from the primary so that they have the same data set. Replica set can have only one primary node which will do write operation. Read operation can be enabled from secondary server but Write operation can only be done from primary node.

Replica set is a group of two or more nodes ( mongo replication needs min of 3 nodes) and one node is primary node and remaining nodes are secondary all data from primary replicates to secondary.

MongoDB Replication tutorial

Configure Replication in MongoDB

To enable replication: first need to create the replica sets of MongoDB instances. First we need to install mongoDB on three nodes, primary and 2 secondary nodes. For installing mongoDB on different OS :- (https://docs.mongodb.com/manual/installation/#tutorial-installation)

On the process of replication it is better to use DNS hostnames other than server IP. After the setup of the mongo instance in all servers make sure the MongoDB instance listens to localhost each replica sets and also with the application. It binds only to localhost by default.

To bind to other ip addresses, use the net.bindIp configuration file setting or the –bind_ip command-line option to specify a list of hostnames or ip addresses.

For example mongo listen to localhost with replicaserver1 we use the command :-

>mongod –bind_ip localhost, replicaserver1

After that check the connection by

>mongo –host replicaserver1

We can also use an IP address instead of hostname.

NOTE: Do the same process on all servers and test Connections Between all Members.

If you have Replicaserver1, Replicaserver2, and Replicaserver3 and Replicaserver1, is defined as the primary server in the replica set.

First we have to specify the replica set name and the IP binding through the — replSet and –bind_ip command.

Syntax:

>mongod –replSet “rs0” –bind_ip localhost,<hostname(s)|ip address(es)>

For <hostname(s)|ip address(es)>, specify the hostname(s) and/or ip address(es) for your mongod members of the replica set.

Example:-

>mongod –replSet “rs0” –bind_ip localhost,replicaserver2,replicaserver3,replicaserver1

We can also specify the replica set name and the IP on configuration file. If we are using a conf file we have to use –conf option to start the mongo.

mongod –config <path-to-config>

Step 2 :- Connect a mongo shell to one of the mongod instances.

We can connect the mongodb from the same machine which listens to localhost and use the default port 2701 by simply using the mongo command.

>mongo

NOTE: Depending on your path, you may need to specify the path to the mongo binary.

Step 3 :- Initiating the replica set.

We have to initiate the rs.initiate() command from the mongo shell of replicaserver1.

Note: Run rs.initiate() on just only one mongod instance for the replica set.

rs.initiate() example (on mongo shell) :-

rs.initiate( {

_id : “rs0”,

members: [

{ _id: 0, host: “replicaserver1:27017” },

{ _id: 1, host: “replicaserver2:27017” },

{ _id: 2, host: “replicaserver3:27017” }

]

})

By using this command, the replica set initiates with default configuration.

View replica set configuration.

Use the rs.conf() command to view the current replica set configuration. The configuration output look like:

>rs.conf()

Output :-

{

“_id” : “rs0”,

“version” : 1,

“protocolVersion” : NumberLong(1),

“members” : [

{

“_id” : 0,

“host” : “replicaserver1:27017”,

“arbiterOnly” : false,

“buildIndexes” : true,

“hidden” : false,

“priority” : 1,

“tags” : {

},

“slaveDelay” : NumberLong(0),

“votes” : 1

},

{

“_id” : 1,

“host” : “replicaserver2:27017”,

“arbiterOnly” : false,

“buildIndexes” : true,

“hidden” : false,

“priority” : 1,

“tags” : {

},

“slaveDelay” : NumberLong(0),

“votes” : 1

},

{

“_id” : 2,

“host” : “replicaserver3:27017”,

“arbiterOnly” : false,

“buildIndexes” : true,

“hidden” : false,

“priority” : 1,

“tags” : {

},

“slaveDelay” : NumberLong(0),

“votes” : 1

}

],

“settings” : {

“chainingAllowed” : true,

“heartbeatIntervalMillis” : 2000,

“heartbeatTimeoutSecs” : 10,

“electionTimeoutMillis” : 10000,

“catchUpTimeoutMillis” : -1,

“getLastErrorModes” : {

},

“getLastErrorDefaults” : {

“w” : 1,

“wtimeout” : 0

},

“replicaSetId” : ObjectId(“585ab9df685f726db2c6a840”)

}

}

In this output we can see that the three replication sets have been synced. Make sure that the replica set has a primary node by using the command rs.status(). After the process, upload the data to the primary node and check the same data is copied between other secondary nodes.

We can add a new replica set by rs.add( eg:- (rs.add(“Server4”)) and remove by rs.remove

(eg:-rs.remove(“Server4”) command.

We can customize the working of the server like read only from secondary node etc.. but the write operation only occurs on the primary set. If the primary set was down, an election process will occur on the remaining secondary sets and a new primary node will be set, Note if the primary node comes back online it is scheduled as secondary so we have to manually make it as primary.

GET STARTED WITH US

FAQ SECTION

What is meant by Mongo DB Server Replication?

Replication is the method of synchronizing data across multiple servers. Replication provides redundancy and availability. A replica set in MongoDB server is a group of mongod processes in multiple DB servers that maintain the same data set, Multiple copies of data on different database servers.

Why is Replication done?

Replication will keep our data safe on multiple servers. Hardware failures and service interruptions will not cause any data loss as there will always be a working server.

Which nodes perform the read and write operations?

During replication, the primary node receives all the write operations. Secondary nodes are an instance of the primary node and do the read operation.

How many nodes will be there in a replica set?

A replica set is a group of two or more nodes. Replication needs a minimum of three nodes. One node will be primary and the rest replicated nodes from the primary node will be the secondary nodes.

How to enable replication in MongoDB?

To enable replication, a replica set of MongoDB instances should be created. MongoDB should be installed on three nodes i.e one primary node and two secondary nodes.

 

Leave a Reply