How to Fix MongoDB Error 95 (NotSecondary)

Published
Length
3 min read

Luis Felipe Dias

All posts

You see mongodb error 95 when a query expects a secondary node, but hits another node. You can fix this NotSecondary failure by routing reads to actual secondary members or updating client preferences. In a replica set, each database node holds a specific operational state. One node acts as the primary and takes all write traffic. Other nodes act as secondaries and copy data from the primary node. When an app asks for a secondary-only task on a primary node, the engine stops the request. It ret

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 95 when a query expects a secondary node, but hits another node. You can fix this NotSecondary failure by routing reads to actual secondary members or updating client preferences.

In a replica set, each database node holds a specific operational state. One node acts as the primary and takes all write traffic. Other nodes act as secondaries and copy data from the primary node. When an app asks for a secondary-only task on a primary node, the engine stops the request. It returns code 95 with the NotSecondary error name to protect cluster stability.

Root causes of mongodb error 95 in replica sets

Several cluster events can trigger mongodb error 95 during daily database operations. The most common cause is a sudden replica set election or failover event. During an election, a secondary node can step up and become the new primary node. If a client connects directly to that single host, its secondary-only requests will start failing.

Direct single-node host strings also cause this issue in production setups. Engineers often point reporting tools or backup jobs at one fixed host address. When the cluster shifts member states during maintenance, that fixed address may no longer be a secondary.

Maintenance work is another frequent trigger for the NotSecondary error code. If an admin restarts a node in standalone mode or recovery state, it leaves the secondary state. Any script or client expecting a secondary member on that port will receive code 95 right away.

How to diagnose and fix NotSecondary failures safely

To fix this error, first check the current state of every member in your replica set. Make sure your cluster has at least one healthy node running in the secondary state. Next, inspect how your app or script connects to the database cluster.

Replace direct single-host strings with a full replica set seed list in your client settings. A replica set seed list lets the client driver discover current node states on its own. When a failover happens, the driver routes secondary read traffic to a valid secondary node.

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

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

If an operational task only needs to read data without taxing the primary, check your read preferences. Selecting a preference that falls back to a primary can prevent hard app crashes during maintenance. Do not grant broad admin rights just to bypass node state checks during an outage.

Keeping role permissions tight during cluster fixes

When teams rush to fix replica set routing, they often hand out broad database credentials. This habit creates severe security risks across production clusters. Instead, keep your user permissions scoped to the exact task at hand.

MongoDB controls access through role-based rules that apply to specific databases and collections. For reporting tools or debugging sessions, assign the built-in read role on the target database. This role allows safe read actions like finding records, listing collections, and checking collection stats. It blocks actions that change data, such as inserting, updating, removing, or dropping collections.

Only grant the readWrite role when a user or service must modify records or build indexes. Note that Atlas database users and self-hosted users have different built-in roles, though both use the same privilege actions. Keeping roles narrow ensures that a misrouted connection cannot alter data on a primary node.

Inspect MongoDB wire traffic in flight

Built-in MongoDB roles govern collection access at setup time. They cannot score command risk per call or mask sensitive document values on the fly. By design, hoop.dev sits inline in the MongoDB protocol next to your server as a sidecar for your MongoDB. On every production command from mongosh, drivers, or AI agents, hoop.dev decides whether to pass, mask, block, or hold it for human review.

First, hoop.dev guardrails block bad deletes, drops, and write pipelines or hold them for approval. Next, hoop.dev data masking rewrites sensitive values in returned documents in memory and logs each decision. Explore the repo on GitHub.

Frequently asked questions

What does MongoDB error code 95 mean?

Error code 95 has the official name NotSecondary in the database engine. It means an operation required a secondary replica set member, but ran on a node in another state.

Why did my backup or reporting script suddenly get NotSecondary?

Your script likely connects to a single host address instead of the full replica set. If that host stepped up to primary during an election, secondary-only commands sent to it will fail.

Does changing user roles fix error 95?

No, error 95 is a node state and routing issue, not a permission denial. Still, using strict read roles ensures that reconnected clients cannot accidentally write data if routed to a primary.

The query runs. The emails stay hidden.