Latest web development tutorials

MongoDB copy (replica set)

MongoDB replication is the process of data synchronization across multiple servers.

Copy provides redundant backup data and store copies of data on multiple servers to improve the availability of data, and can guarantee the security of the data.

Copy also allows you to recover data from hardware failures and service interruption in.

What is a copy?

  • Data security safeguards
  • High data availability (24 * 7)
  • Disaster Recovery
  • No downtime maintenance (such as backup, rebuild the indexes, compression)
  • Distributed data is read

MongoDB Replication Works

mongodb replication requires at least two nodes. One of the primary node is responsible for handling client requests, and the rest are from the node responsible for replicating the data on the primary node.

mongodb each node common with the way: a master-slave, a master multi-slave.

Master record on which all operations oplog, obtained from the master node periodically poll these operations, then perform these operations on their own copy of the data from the data in order to ensure consistency with the master node.

MongoDB replicated structure is shown below:

MongoDB replication chart

The above structure of the total, total client master node reads the data, the client data is written to the primary node is the master node and data exchange to ensure data consistency from the node.

Replica set features:

  • N cluster nodes
  • Any node can be used as the master node
  • All write operations on the primary node
  • Automatic failover
  • Automatic recovery

MongoDB replica set settings

In this tutorial, we use the same master-slave MongoDB MongoDB do experiments by following these steps:

1. Close MongoDB server is running.

Now let's start by specifying mongoDB --replSet option. --replSet basic syntax is as follows:

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


mongod --port 27017 --dbpath "D:\set up\mongodb\data" --replSet rs0

The above examples will start MongoDB instance called rs0, the port number is 27017.

Open a command prompt after starting box and connected mongoDB service.

In Mongo client uses the command rs.initiate () to start a new replica set.

We can use rs.conf () to view the configuration of the replica set

Check the status of a replica set using rs.status () command

Replica set Add Members

Add a member of the replica set, we need to use multiple servers to start mongo service. Enter Mongo client and use rs.add () method to add members of the replica set.


rs.add () command basic syntax is as follows:


Suppose you have started a group called, port number 27017 of Mongo service. In the client command window using rs.add () command to add it to the replica set, the command is as follows:


MongoDB you can only master node adds Mongo service to the replica set, it is determined whether the service is currently running Mongo primary node can use the command db.isMaster ().

MongoDB replica set with our common master and slave is different from the master after the host is down all services will be stopped, and after the host replica set down, a copy will take over the primary node becomes the master node and will not appear downtime Happening.