MongoDB refuses a read because the node is a secondary
not primary and secondaryOk=falseShort 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.
Fix it fast
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()2Match what you see, then jump to its fix
- 1
The error appears on reads, and db.hello() shows isWritablePrimary: falseConnected straight to a secondary with the default read preferenceFix - 2
It 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
You still run the legacy mongo shell, and rs.slaveOk() was your old fixThe old mongo shell needs setReadPrefFix
- 1
What does the error look like?
not primary and secondaryOk=falsenot 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().isWritablePrimary2Fix
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
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_URI2Fix
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=myRepl3Verify
Connect with the new URI and run a write.
mongosh "$MONGODB_URI"
db.hello().isWritablePrimaryConfirmed by mongodb.com
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 mongosh2Fix
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
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
Databases
Installing the Airflow MySQL provider fails on pkg-config or mysqlclient
Building the mysqlclient wheel fails during the install
The Airflow MySQL provider depends on mysqlclient, which pip compiles against the MySQL C client library.
2 causes, each with a fixDatabases
asyncpg InterfaceError: another operation is in progress with SQLAlchemy
InterfaceError raised from a FastAPI endpoint that runs several queries at once
A single database connection can run one operation at a time.
4 causes, each with a fixDatabases
A query on Aurora MySQL dies with 'table /rdsdbdata/tmp/#sql... is full'
ERROR 1114 (HY000): The table '/rdsdbdata/tmp/#sqlfd_1586_2' is full
In Aurora MySQL 3, internal temporary tables share one pool, and when that pool and the memory-mapped overflow file are used up, a reader instance ends the query with error 1114.
3 causes, each with a fixDatabases
Azure Database for MySQL refuses a connection that does not use SSL
ERROR 3159 (HY000): Connections using insecure transport are prohibited while --require_secure_transport=ON.
Azure Database for MySQL Flexible Server requires encrypted connections by default (require_secure_transport = ON, TLS 1.2).
4 causes, each with a fixDatabases
Compiling a C or C++ program that includes mysql.h fails
undefined reference to `mysql_init'
The compiler only finds mysql.h when the MySQL client development files are installed and their folder is on the include path.
3 causes, each with a fixDatabases
Dapper cannot find a constructor that fits the query columns
The error appears when Dapper maps rows into a record or a class whose only constructor takes arguments
Dapper looks for a public parameterless constructor first.
3 causes, each with a fix