Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

MongoDB refuses a read because the node is a secondary

Errornot primary and secondaryOk=false

Short answer

The node you are connected to is a secondary, and your connection uses the default read preference, primary. Reads are refused. Set a read preference such as secondaryPreferred in the connection string or in mongosh, or connect through the replica set so the driver finds the primary.

  • Applies to MongoDB 8.0, mongosh 2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask the node whether it is the primary. If isWritablePrimary is false, you are on a secondary.

    mongosh "mongodb://host1:27017"
    db.hello()
  2. 2Match what you see, then jump to its fix

    1. 1The error appears on reads, and db.hello() shows isWritablePrimary: falseConnected straight to a secondary with the default read preferenceFix
    2. 2It worked yesterday; after a failover the same URI fails, or only writes failThe client uses directConnection=true against a member that is no longer primaryFix
    3. 3You still run the legacy mongo shell, and rs.slaveOk() was your old fixThe old mongo shell needs setReadPrefFix

What does the error look like?

  • not primary and secondaryOk=false
  • not master and slaveOk=false
  • "code" : 13435

Cause 1 of 3

Connected straight to a secondary with the default read preference

1Check

By default reads go to the primary. A direct connection to a secondary with read preference primary is refused.

mongosh "mongodb://host2:27017/?directConnection=true"
db.hello().isWritablePrimary

2Fix

Give the connection a read preference that allows secondaries: primaryPreferred, secondary or secondaryPreferred. In mongosh you can set it per session. rs.secondaryOk() is no longer required.

mongosh "mongodb://host2:27017/?directConnection=true&readPreference=secondaryPreferred"

// or inside mongosh
db.getMongo().setReadPref("secondaryPreferred")

3Verify

Run the same query again.

db.getCollection('items').find().limit(1)

Confirmed by mongodb.com

Did this fix it?

Cause 2 of 3

The client uses directConnection=true against a member that is no longer primary

1Check

With directConnection=true the client sends operations only to that host and never looks for the primary. The default, false, discovers members and sends operations to the primary.

echo $MONGODB_URI

2Fix

List the members and name the replica set in the URI, and remove directConnection=true, so the driver follows the primary after an election.

mongodb://db0.example.com:27017,db1.example.com:27017,db2.example.com:27017/?replicaSet=myRepl

3Verify

Connect with the new URI and run a write.

mongosh "$MONGODB_URI"
db.hello().isWritablePrimary

Confirmed by mongodb.com

Did this fix it?

Cause 3 of 3

The old mongo shell needs setReadPref

1Check

The legacy shell needs setReadPref to read from a secondary. mongosh replaces it.

which mongo mongosh

2Fix

Move to mongosh, or in the legacy shell call setReadPref.

db.getMongo().setReadPref("secondaryPreferred")

3Verify

Repeat the failed query.

db.getCollection('items').count()

Confirmed by mongodb.com

Did this fix it?

How do I stop it coming back?

  • Connect with a full replica set URI, not one host, in application code.
  • Use secondary reads only for data that can be a little stale; the manual warns that every mode except primary can return stale data.
  • Run db.hello() when a connection behaves strangely, to see which node you are on.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.