Error
Error Code: 123

MongoDB Error 123: Not A Replica Set

📦 MongoDB
📋

Description

This error indicates that an operation requiring a replica set context was attempted on a MongoDB instance that is not configured or recognized as part of a replica set. It commonly occurs when a client expects replica set behavior from a standalone `mongod` server. The instance is operating in a non-replica set mode.
💬

Error Message

Not A Replica Set
🔍

Known Causes

3 known causes
⚠️
Standalone MongoDB Instance
The `mongod` process was started without the `--replSet` option, meaning it operates as a standalone server, not part of a replica set.
⚠️
Replica Set Not Initialized
The `mongod` instance was configured with `--replSet` but the replica set itself has not been initiated using `rs.initiate()` or `rs.add()` commands.
⚠️
Mismatched Client Configuration
The client application is configured to connect to a replica set (e.g., using `?replicaSet=myReplicaSet` in the connection URI) but the target MongoDB instance is standalone.
🛠️

Solutions

3 solutions available

1. Start a New Replica Set easy

Initialize a new replica set if the instance was not intended to be part of one.

1
Ensure the MongoDB instance is not running. Stop it if it is.
2
Modify the MongoDB configuration file (e.g., mongod.conf) to include replica set settings. Add or uncomment the `replication` section and specify a `replSetName`.
replication:
  replSetName: "myReplicaSetName"
3
Start the MongoDB instance with the updated configuration.
mongod --config /path/to/your/mongod.conf
4
Connect to the MongoDB instance using the mongo shell.
mongo
5
Initiate the replica set configuration.
rs.initiate()
6
Verify the replica set status. You should see the primary and secondary members.
rs.status()

2. Connect to a Replica Set Member easy

Ensure your application or client is configured to connect to a replica set, not a standalone instance.

1
Identify the hostname and port of at least one member of the intended replica set.
2
When connecting using the `mongo` shell or a MongoDB driver, provide the replica set name and a comma-separated list of replica set members.
mongo --host "mongo1.example.com:27017,mongo2.example.com:27017" --replicaSet "myReplicaSetName"
3
Ensure your application's MongoDB connection string includes the `replicaSet` parameter.
mongodb://mongo1.example.com:27017,mongo2.example.com:27017/?replicaSet=myReplicaSetName

3. Remove Replica Set Configuration (for Standalone Use) medium

If the instance was mistakenly configured as a replica set and you want it to be standalone, remove the replica set configuration.

1
Connect to the MongoDB instance using the `mongo` shell.
mongo
2
If the instance is currently a primary, attempt to step down to allow other members to become primary (if applicable). This step might not be necessary if you are sure it's the only instance.
rs.stepDown()
3
Stop the MongoDB instance.
4
Edit the MongoDB configuration file (e.g., mongod.conf). Remove or comment out the `replication` section, including the `replSetName`.
#replication:
#  replSetName: "myReplicaSetName"
5
Start the MongoDB instance with the updated configuration.
mongod --config /path/to/your/mongod.conf
6
Verify that the instance is now running as a standalone server by checking its status or running `rs.status()` which should now throw an error.
rs.status()
🔗

Related Errors

5 related errors