Reprinted with attribution:
In MongoDB, thers
(often referred to as an abbreviation for "replica set") is an identifier for a replica set or a common prefix when used, especially when referring to replica sets in command line tools and scripts. A replica set is a core component of MongoDB used for data redundancy and high availability.
The role of the Replica Set (Replica Set):
-
Data Redundancy: A replica set maintains multiple copies of the same data on multiple servers to prevent data loss due to a single point of failure.
-
High Availability: Through the automatic failover mechanism, when the primary node is unavailable, one of the slave nodes in the replication set will be elected as the new primary node, thus maintaining service continuity.
-
Read Extension: Clients can distribute read operations to slave nodes, thereby reducing the pressure on the master node and improving overall read performance.
How to use replica sets:
1. Initializing the replica set
In MongoDB, you can pass the()
command initializes a new replica set in the mongo shell. Let's say you have MongoDB instances running on three different servers and want to configure them as a replica set.
# Login to the mongo shell of one of the MongoDB instances
mongo
# Switch to the admin database (or whatever database you wish to use for your replica set configuration)
use admin
# Initialize the replica set
({
_id."rs0",
members: [
{ _id: 0, host: "mongodb-host1:27017" },
{ _id: 1, host: "mongodb-host2:27017" },
{ _id: 2, host: "mongodb-host3:27017" }
]
})
Here._id
is the name of the replica set (used herers0
(as an example).members
array lists all members of the replication set with their hostnames and ports.
2. Addition of replica set members
To add a new member to an existing replica set, you can use the following command:
(":27017")
3. Deletion of replica set members
To remove a member from a replica set, use the following command:
(":27017")
4. Monitor replica set status
utilization()
command to obtain the current state of the replica set, including the state of each member, replication delay, and other information.
use admin
()
5. Read and write operations
By default, MongoDB clients connect to the master node in a replica set for write operations and can be configured to read data from slave nodes to reduce the load on the master. This is typically accomplished through the Read Preference setting in the connection string.
# Connect to replica set using mongo shell (automatically connects to master node)
mongo mongodb.//mongodb-host1:27017,mongodb-host2:27017,mongodb-host3:27017/yourdatabase?replicaSet=rs0
# In your application, you may need to set read preferences to read data from slave nodes
# This depends on the MongoDB driver or client library you use
6. Failover and recovery
If the master node fails, the other members of the replication set will detect this and initiate the failover process. One of the slave nodes will be elected as the new master node and the application's connection (if auto-reconnect is configured) will be automatically switched to the new master node.