Replica Set in MongoDB

Replication is the process of synchronizing data from multiple servers. Replication provides redundancy and increased data availability for multiple data copies on many different Database Servers. Replication protects a database from the loss of a particular Server. Replication also allows you to recover data from hardware errors or service disconnections. With additional data copies, you can use it for recovery, reporting, or backup.

Why use Replication?

To make your data safe.

High availability of data (24 * 7).

Recover data due to an error.

It doesn't take time to maintain (like backup, rebuild the index).

Expand readability (read from additional copies).

Replica Set is transparent for the application.

How Replication works in MongoDB

MongoDB uses Replica Set to implement Replication. A Replica Set is a group of mongodb instances that host the same data. In a Replica, a node that is Primary node (can be called a secondary node) will receive all write operations. All other, secondary instances, apply operations from the secondary node so that they have the same data set. Replica Set can have only one secondary node.

  1. Replica Set is a group of two or more nodes (generally, need at least 3 nodes).
  2. In a Replica Set, a node is a secondary node and the other nodes are primary.
  3. All data replicated from primary node to secondary node.
  4. At the time of automatic maintenance, the selection for the primary and a primary node is selected.
  5. After the node recovery has failed, it again combines Replica Set and works as a secondary node.

Below is a Replication-specific diagram in MongoDB, in which the Client application always interacts with the primary node and this primary node then recreates the data for the secondary node.

Replica Set in MongoDB Picture 1

Features of Replica Set

A Cluster consists of N nodes

Any node can be primary

All activities are recorded in elementary level

Automatically maintained

Auto recovery

Install a Replica Set

In this chapter, we will convert the instance to a Replica Set. To convert to Replica Set, follow these steps:

Turn off MongoDB Server running.

Now, start MongoDB Server with the --replSet option specified . --ReplSet 's basic syntax is as follows:

 mongod -- port "PORT" -- dbpath "YOUR_DB_DATA_PATH" -- replSet "REPLICA_SET_INSTANCE_NAME" 

For example

 mongod -- port 27017 -- dbpath "D:set upmongodbdata" -- replSet rs0 

It will start an instance with the name rs0, on port 27017. Now, start the command line and connect to this instance. In the Mongo Client notice the rs.initiate () command to initialize a new Replica Set. To check the Replica Set configuration, you notice the command rs.conf () . To check the status of Replica Set, you notice the command rs.status () .

Add members to Replica Set

To add members to Replica Set, start instance descriptions on multiple devices. Now, start a Mongo Client and notice a command rs.add ().

Syntax

The basic syntax of rs.add () is as follows:

 > rs . add ( HOST_NAME : PORT ) 

For example

Suppose the name of your mongod instance is mongod1.net and is running on port 27017 . To add this instance to Replica Set, you tell rs.add () in Mongo Client:

 > rs . add ( "mongod1.net:27017" ) > 

You can add an instance to Replica Set only if you are connected to the primary node. To check if you have connected to this primary node, you report the db.isMaster () command in Mongo Client.

According to Tutorialspoint

Previous article: Aggregation in MongoDB

Next article: Shard in MongoDB

4.3 ★ | 6 Vote

May be interested

  • Shard in MongoDBShard in MongoDB
    sharding is a process of storing data records across multiple devices and it is a method of mongodb to meet the requirement for increasing data. when the size of the data increases, a single device cannot be enough to store data.
  • Learn about Java Driver in MongoDBLearn about Java Driver in MongoDB
    in the following article, we will introduce you some basic features of mongodv java driver as well as how to deploy and apply in practice.
  • Install MongoDBInstall MongoDB
    instructions for installing mongodb on windows.
  • Map Reduce in MongoDBMap Reduce in MongoDB
    in mongodb documentation, map-reduce is a data processing system that condenses a large amount of data into useful overall results. mongodb uses mapreduce command for map-reduce operation. in general, map reduce is used to handle large data sets.
  • Aggregation in MongoDBAggregation in MongoDB
    aggregation can be understood as aggregation. the aggregation operation handles data records and returns calculated results. the operations group the values ​​from multiple documents together, and can perform multiple operations on the grouped data to return a single result. in sql, count (*) and group by are equivalent to aggregation in mongodb.
  • Data modeling in MongoDBData modeling in MongoDB
    data in mongodb has a flexible schema. documents in the same collection need not have the same set of fields or structures, and common fields in collection documents can keep different data types.
  • How to use Aggregation Pipeline in MongoDBHow to use Aggregation Pipeline in MongoDB
    if you are using mongodb's mapreduce, it is best to switch to aggregation pipeline for more efficient computation.
  • GridFS in MongoDBGridFS in MongoDB
    gridfs is the mongodb specification for storing and collecting large files such as images, audio, video files, etc. it is a type of file system to store files but its data is stored inside mongodb collections. .
  • Query analysis in MongoDBQuery analysis in MongoDB
    analyzing queries is a very important aspect to assess the effectiveness of database and the effectiveness of the designed index. we will explore the two most frequently used queries, $ explain and $ hint.
  • Projection in MongoDBProjection in MongoDB
    in mongodb, projection's meaning is to select only the necessary data instead of selecting the entire data of a document. if a document has 5 fields and you only need 3 fields, you should only select 3 fields from that document.