Error
Error Code: 71

MongoDB Error 71: Replica Set Not Found

📦 MongoDB
📋

Description

This error indicates that a MongoDB instance, configured to be part of a replica set, cannot locate or properly connect to the specified replica set. It typically occurs when a `mongod` process starts up, attempts to join, or performs operations within a replica set environment, but finds the configuration or network state prevents it from doing so.
💬

Error Message

Replica Set Not Found
🔍

Known Causes

4 known causes
⚠️
Incorrect Replica Set Name
The `replSet` parameter in the `mongod.conf` file or command line options does not match the actual, expected replica set name.
⚠️
Missing `replSet` Parameter
A `mongod` instance intended for a replica set was started without the `--replSet` command-line option or `replication.replSetName` in its configuration file.
⚠️
Network Connectivity Problems
The `mongod` instance cannot reach other members of the replica set due to firewall rules, incorrect IP addresses, or network outages.
⚠️
Replica Set Not Initiated
The replica set has been configured but has not yet been initiated using `rs.initiate()` or its configuration was reset/deleted.
🛠️

Solutions

4 solutions available

1. Verify Replica Set Configuration on All Members medium

Ensure all MongoDB instances are correctly configured to be part of the replica set.

1
Connect to each MongoDB instance that should be part of the replica set using the `mongo` shell.
mongo --host <hostname> --port <port>
2
Run the `rs.status()` command. This command will show the status of the replica set. If a member is not recognized as part of the replica set, this command might return an error or an incomplete status.
rs.status()
3
If `rs.status()` indicates the node is not part of the replica set, check the `mongod.conf` (or `mongod.yaml`) configuration file on that specific instance. Ensure the `replication.replSetName` parameter is set to the correct replica set name.
# Example mongod.conf snippet
replication:
  replSetName: myReplicaSetName
4
If the `replSetName` was incorrect or missing, update the configuration file and restart the `mongod` service on that instance.
# For systemd-based systems
sudo systemctl restart mongod

# For older init systems
sudo service mongod restart
5
After restarting, connect to the primary member (if known) and run `rs.status()` again to confirm all members are now recognized.
rs.status()

2. Initiate Replica Set if Not Already Done medium

If the replica set has not been properly initialized, this error can occur.

1
Connect to the MongoDB instance that is intended to be the initial primary member of your replica set.
mongo --host <hostname> --port <port>
2
Execute the `rs.initiate()` command. This command initializes the replica set. You will need to provide a configuration document that lists all members of the replica set.
rs.initiate({
  _id: "myReplicaSetName",
  members: [
    { _id: 0, host: "mongo1.example.net:27017" },
    { _id: 1, host: "mongo2.example.net:27017" },
    { _id: 2, host: "mongo3.example.net:27017" }
  ]
})
3
Replace `myReplicaSetName` with your desired replica set name and the `host` values with the actual hostnames and ports of your MongoDB instances.
text
4
Wait for the replica set to stabilize. You can check the status using `rs.status()`.
rs.status()

3. Check Network Connectivity and Firewall Rules easy

Ensure that MongoDB instances can communicate with each other on the necessary ports.

1
Identify the ports that your MongoDB instances are running on (default is 27017).
text
2
From each MongoDB server, attempt to connect to the other MongoDB servers on their respective ports using `telnet` or `nc` (netcat).
# From mongo1, test connection to mongo2
telnet mongo2.example.net 27017
3
If the connection fails, check your firewall rules on both the client and server machines to ensure that traffic on the MongoDB port is allowed between the replica set members.
# Example using ufw (Ubuntu)
sudo ufw allow from <ip_of_other_member> to any port 27017
sudo ufw reload
4
Verify that DNS resolution is working correctly if you are using hostnames. You can use `ping` or `nslookup` to test this.
ping mongo2.example.net

4. Reconfigure Replica Set Members with `rs.reconfig()` advanced

If members are incorrectly added or removed, reconfiguring the replica set can resolve the issue.

1
Connect to the primary member of the replica set.
mongo --host <primary_hostname> --port <primary_port>
2
Fetch the current replica set configuration.
cfg = rs.conf()
3
Modify the `cfg` object to reflect the desired state of your replica set. This might involve adding, removing, or updating member configurations. Be extremely careful when modifying this object.
# Example: remove a member
cfg.members.splice(index_of_member_to_remove, 1)

# Example: add a member
cfg.members.push({_id: new_id, host: "new_member_host:port"})
4
Apply the new configuration using `rs.reconfig()`. This command will restart the election process if necessary.
rs.reconfig(cfg)
5
Verify the replica set status after reconfiguring.
rs.status()
🔗

Related Errors

5 related errors