How to Fix MongoDB Error 155 (CannotApplyOplogWhilePrimary)

Published
Length
3 min read

Luis Felipe Dias

All posts

You get mongodb error 155 when a primary node tries to apply oplog entries. To fix it, stop oplog replay on the primary or step the node down. In a replica set, only secondary nodes or standalone restoration targets apply oplog entries. The official error name for this issue is CannotApplyOplogWhilePrimary. The database engine blocks this action to protect cluster data from corruption and loops. Root causes of mongodb error 155 in replica sets A primary node accepts client writes and records

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 get mongodb error 155 when a primary node tries to apply oplog entries. To fix it, stop oplog replay on the primary or step the node down.

In a replica set, only secondary nodes or standalone restoration targets apply oplog entries. The official error name for this issue is CannotApplyOplogWhilePrimary. The database engine blocks this action to protect cluster data from corruption and loops.

Root causes of mongodb error 155 in replica sets

A primary node accepts client writes and records them in its own oplog. Secondary nodes read that oplog and apply the changes to stay in sync. If a script tries to replay oplog operations on a primary, the engine stops it.

This problem often starts during manual point-in-time recovery or data migration tasks. An engineer might connect a recovery script to the wrong host IP address. If that host is the current primary, the restore job fails right away.

Network splits and sudden elections can also trigger this failure during maintenance. You might start an oplog replay on a secondary node during a repair window. If the primary fails, the cluster might elect your target node as the new primary. As soon as the node becomes primary, the database rejects the next oplog batch.

How to diagnose and fix the failure in production

First, check the replica set status to see which node holds the primary role. Look at your restore or sync job logs to verify the target host. Never bypass safety checks or force writes directly into system collections on a primary.

If you are running a point-in-time restore, point your tool to a standalone instance. You can also step down the target node or restart it in maintenance mode. Once the node is not primary, it can apply the oplog entries safely.

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 unexpected election caused the error, pause your maintenance job right away. Step the node down and stop it from winning elections during your repair work. Wait until the oplog replay finishes before you let the node rejoin normal traffic.

Preventing replay mistakes with safer role design

Many oplog errors happen because teams use broad admin accounts for everyday scripts. MongoDB grants access through role-based authorization on a specific database. A role grants privileges to perform sets of actions on defined resources. It can grant access down to a collection level of granularity.

For daily diagnostics, assign the built-in read role to your team members. The read role gives access to non-system collections through find and stats actions. It does not allow insert, update, remove, or drop actions on the database.

When an app needs to change data, use the standard readWrite role instead. Neither role allows a user to run internal oplog replay commands on a cluster. By restricting high-level admin privileges, you stop accidental oplog writes before they hit production.

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 CannotApplyOplogWhilePrimary mean in plain terms?

It means a user or process tried to apply replication log entries to a primary node. Primary nodes only generate new oplog entries, so they refuse to replay incoming ones.

Does mongodb error 155 cause data loss on the primary node?

No, the database throws this error to prevent data loss and write conflicts. The engine stops the bad operation before it can overwrite live primary data.

How can I replay an oplog for point-in-time recovery safely?

Run your oplog replay against a standalone node or a dedicated recovery instance. Once the data is restored and checked, you can sync it back to your cluster.

The query runs. The emails stay hidden.