How to Fix MongoDB Error 123 (NotAReplicaSet)

Published
Length
3 min read

Luis Felipe Dias

All posts

You see mongodb error 123 when a node gets a replica set command while running alone. To fix this NotAReplicaSet error, add replica set settings to the node or change your client call. Many database features need a replica set to work. When an app asks a single database node to run replica set tasks, the database stops the request. It returns code 123 with the NotAReplicaSet name. Knowing why this happens helps your team fix the issue fast. Root causes of mongodb error 123 in your cluster Th

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You see mongodb error 123 when a node gets a replica set command while running alone. To fix this NotAReplicaSet error, add replica set settings to the node or change your client call.

Many database features need a replica set to work. When an app asks a single database node to run replica set tasks, the database stops the request. It returns code 123 with the NotAReplicaSet name. Knowing why this happens helps your team fix the issue fast.

Root causes of mongodb error 123 in your cluster

The database engine throws mongodb error 123 when a server runs without a replica set name. A client or admin script might ask this server for replica set status. Because the server runs as a single instance, it rejects the command right away.

This failure often happens during initial server setup or after a bad restart. An admin might start a node and leave out the replica set option. If the rest of the cluster expects that node to join, calls to it will fail.

Client connection strings also trigger this problem in test and staging systems. Your app might include replica set requirements or change stream requests in its calls. The built-in read role includes the changeStream action for reading non-system collections. Still, change streams still need a running replica set to work. When the app sends these requests to a single test database, code 123 appears.

Multi-document transactions can also cause this error when run on a single node. Developers often build apps on local single nodes before moving to a full cluster. When the app tries a feature that needs replication logs, the single server blocks it.

How to diagnose and fix the failure in production

Start by checking if the target server should be part of a replica set. Look at your server startup settings to see if a replica set name exists. If the server should be a single node, update your app settings instead. Remove replica set options from the client so it talks to the single node properly.

MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

If the server must run as a replica set, update its startup settings. Add the right replica set name and restart the database service during a safe window. After the restart, initiate the replica set or join the node to your existing group. For local testing, a single node replica set lets you test change streams and transactions.

Do not give engineers broad admin rights just to check or fix this error. MongoDB uses role-based access to grant actions on resources down to the collection level. Give your team only the specific roles needed to check server state. The built-in read role grants actions like find, listCollections, listIndexes, dbStats, and collStats.

Avoid using the readWrite role when an engineer only needs to inspect the database. The readWrite role adds write actions like insert, update, remove, createCollection, and dropCollection. Keeping roles tight prevents mistakes while your team fixes the replica set state.

Preventing replica set configuration drift

Config drift causes most unexpected NotAReplicaSet errors in live systems. When one node restarts with an old file, it drops out of the replica set. Use automated tools to manage your startup files across every database server.

Test your client settings against single-node replica sets in your staging setups. This makes your test systems match your live cluster behavior. It catches code 123 issues before your team deploys new app code to users. Also check that self-hosted and cloud roles match your security rules.

Check MongoDB commands and documents on the wire

MongoDB roles and SCRAM login decide who opens a session. They do not check single commands or redact sensitive fields inside returned documents. With hoop.dev, you run an open-source proxy, a sidecar for your MongoDB, inline in the MongoDB wire protocol. On every command an AI agent or engineer runs against production, hoop.dev decides to pass, mask, block, or hold it for human review.

Before a command runs, hoop.dev guardrails check its effect and refuse banned drops or writes with MongoDB error code 13, or hold risky commands for approval. On the way back, hoop.dev data masking rewrites sensitive fields in returned documents in memory, and every verdict is logged. See the open-source code on GitHub.

Frequently asked questions

What does NotAReplicaSet mean in MongoDB?

NotAReplicaSet is the official name for error code 123 in the database engine. It means a command needed a replica set, but the server runs as a standalone node.

Can the read role run change streams on a standalone server?

No, even though the read role grants the changeStream action, the server still needs replication. You must convert the standalone node into a replica set to use change streams.

How do I fix error 123 without taking down my app?

If your app talks to a standalone node by mistake, point it to your active replica set. If a secondary node lost its settings, fix its config file and restart that node alone.

The query runs. The emails stay hidden.