dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Home/Error Library/mongodb-not-writable-primary
MongoDB
MongoDB 8.0 / 7.0 / 6.0 / 5.0Replication & HA
8 min read
MongoServerError: not primary and secondaryOk=false (NotWritablePrimary, code 10107) / not master

MongoDB "not master" — Why Does Application Fail to Write After Failover?

Troubleshooting MongoDB "not master" (legacy < 5.0) and NotWritablePrimary (code 10107 >= 5.0) during replica set failovers, connection string topology URIs, retryWrites, and oplog sizing.

Reviewed by:Senior NoSQL Reliability Architect
Tested Versions: MongoDB 8.0 & 7.0 (NotWritablePrimary code 10107) | MongoDB 4.4 ("not master")

Operational Verification & Reliability Metadata

Last Technical Review: 29 Ekim 2026
Tested VersionsMongoDB 8.0 & 7.0 (code 10107) | Tarihsel: MongoDB 4.4, 5.0, 6.0
Required PrivilegesclusterMonitor or readAnyDatabase role for rs.status() and URI topology updates
Risk Assessment
HighSingle-host connection URIs permanently reject write operations after replica stepdown
Expected OutputTransparent driver failover failover re-routing under replicaSet URI parameters and retryWrites
Rollback Procedure:Recycle client application pools or orchestrate primary transition with rs.stepDown()
MongoDB Manual: Replica Set Status & Failover
First 60 Seconds Emergency Protocol:

The error string is a symptom, not the root cause. Do not restart the server blindly. Run the diagnostic queries below to pinpoint the actual failure mode.

▸Symptoms

plaintext
MongoServerError: not primary and secondaryOk=false - consider using readPreference=primary. (NotWritablePrimary, code 10107)

▸Diagnosis & Resolution

  1. 1Topology Connection String:
plaintext
mongodb://h1:27017,h2:27017,h3:27017/app?replicaSet=rs0&retryWrites=true&w=majority
  1. 1Inspect Replica Set Health:
javascript
rs.status().members.map(m => ({ name: m.name, state: m.stateStr, health: m.health })); db.hello();
  1. 1Check Oplog Window:
javascript
rs.printReplicationInfo();