Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

MongoDB: not primary and secondaryOk=false (NotPrimaryNoSecondaryOk) and how to fix it

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

The error not primary and secondaryOk=false means a read or command reached a replica set member that is not the primary (usually a secondary), and the request did not allow a secondary to answer it. The server code behind it is NotPrimaryNoSecondaryOk. Check which member your connection string actually points at, then either connect to the whole replica set so the driver finds the primary, or set a secondary read preference because you want a secondary read.

What the symptom means

A replica set has one primary and one or more secondaries. By default clients read from the primary. The MongoDB manual says of the default mode: "Operations read from the current replica set primary."

This wording comes from the MongoDB source code (release r8.0.38), not from the manual. In that source code, the message not primary and secondaryOk=false is returned with the error code NotPrimaryNoSecondaryOk when a member cannot take the request as a primary and the request has not opted in to a secondary. Older clients get the wording not master and slaveOk=false from the same code. A request that did opt in to secondary reads gets a different message if the member is not a primary or a secondary at all: not primary or secondary; cannot currently read from this replSet member. The source code returns it when the member is in a state that is neither primary nor secondary.

The server discovery and monitoring specification that drivers follow describes the direct-connection case: a client must be able to connect to a single server of any type, and "the secondary will reject all operations done with the PRIMARY read preference because the secondaryOk bit is not set, but the initial connection itself succeeds."

So the connection works and the first query fails. The member you connected to was a secondary, or became one after a failover or step-down.

Likely causes

Cause What you see How to confirm
The driver treats one host as a single server Works until that host stops being the primary, then queries fail The string has directConnection=true, or the driver's own convention treats one host as a single server (the specification says such conventions vary); db.hello() on that host shows isWritablePrimary false
A failover or step-down moved the primary The same application worked yesterday rs.status() shows another member as PRIMARY
mongosh connected straight to a secondary with the default read preference Reads in the shell fail on that member db.getMongo().getReadPrefMode() prints primary
The read preference is set on the connection but not on the command Setting it in the shell does not help for db.runCommand() The manual says db.runCommand() ignores a global read preference in mongosh 2.0 and later

Prove the cause before changing anything

All of these are read-only.

1. Which member is primary? On any member:

rs.status()

rs.status() returns the replica set status from the point of view of the member where it runs. Read members[n].stateStr for each member. The manual's example output shows "stateStr" : "PRIMARY" and "stateStr" : "SECONDARY", and myState is 1 for a primary and 2 for a secondary in the manual's replica set states table.

2. What does the member you connect to say about itself?

db.hello()

The hello output has isWritablePrimary, which the manual says "will be false if the instance is a secondary member of a replica set". For a replica set member it also has secondary, primary (the current primary as hostname:port), hosts and setName. If isWritablePrimary is false and primary names a different host, the application is on the wrong member.

3. What is the shell's read preference?

print(db.getMongo().getReadPrefMode());

The manual says this prints the current mode, one of primary, primaryPreferred, secondary, secondaryPreferred or nearest.

4. Read the application's connection string. Look for replicaSet=, the list of hosts and directConnection. The manual says to "Set the replicaSet connection option to ensure consistent behavior across drivers" and, when connecting to a replica set, to "provide a seed list of the replica set members". The specification says that with directConnection=true the starting topology is Single, with or without replicaSet.

Fix

Choose by intent. If the application should always talk to the current primary, fix the connection string. If it should read from a secondary, say so.

The application should follow the primary

Use the replica set form from the connection string options page: every member in the seed list, then the set name.

First save the current settings file. The path here is an example; use the file that holds your application's connection string:

cp -p /etc/webapp/database.env /etc/webapp/database.env.2026-11-22

Disruptive step: the next edit changes where the application sends all of its reads and writes, and the restart that follows drops its open database connections and sessions.

Edit the connection string so it looks like this, with your own host names and set name. Remove directConnection=true if it is there:

mongodb://mongo01.example.com:27017,mongo02.example.com:27017,mongo03.example.com:27017/appdb?replicaSet=rs0

Then restart the application. The unit name here is an example:

systemctl restart webapp

To back out: put the saved file back under its own name and restart again.

cp -p /etc/webapp/database.env.2026-11-22 /etc/webapp/database.env
systemctl restart webapp

The application should read from a secondary on purpose

Reporting or export jobs can read from secondaries. The connection string options page shows the option as readPreference=secondary after replicaSet. The manual warns that "All read preference modes except primary may return stale data because secondaries replicate operations from the primary in an asynchronous process", so confirm the job can tolerate data that is slightly behind. The manual also says the modes give no guarantee: "Read preference modes provide no guarantee that an operation succeeds."

First save the current settings file of the reporting job. The path is an example:

cp -p /etc/reportjob/database.env /etc/reportjob/database.env.2026-11-22

Disruptive step: the next edit changes where the reporting job reads from, to a secondary that may be behind the primary, and the restart that follows drops its open database connections.

Edit the connection string so it looks like this, with your own host names and set name, then restart the job (the unit name is an example):

mongodb://mongo01.example.com:27017,mongo02.example.com:27017,mongo03.example.com:27017/appdb?replicaSet=rs0&readPreference=secondary
systemctl restart reportjob

To back out: put the saved file back under its own name and restart again.

cp -p /etc/reportjob/database.env.2026-11-22 /etc/reportjob/database.env
systemctl restart reportjob

A shell session on a secondary

In the legacy shell this was rs.secondaryOk(). The mongosh compatibility page says that call is "No longer required" and that, connected directly to a secondary, you can read if the read preference is primaryPreferred, secondary or secondaryPreferred.

First print the current mode and note what it prints:

print(db.getMongo().getReadPrefMode());

Risk: the next command changes the read preference for the whole shell connection until you set it back. Queries then run on a secondary and can return stale data.

db.getMongo().setReadPref('secondary')

The manual says to call it on the connection before retrieving documents, and that it "implicitly allows reads from secondaries". It also says that starting in mongosh 2.0 db.runCommand() ignores a global read preference, so pass the preference in the options argument of db.runCommand() if a command still fails.

To undo it: set the mode you noted before, for example db.getMongo().setReadPref('primary') if it printed primary.

Example (illustrative): the rs0 set has mongo01, mongo02 and mongo03. A reporting service uses mongodb://mongo02.example.com:27017/appdb?directConnection=true. After a step-down, rs.status() shows mongo01 as PRIMARY and mongo02 as SECONDARY, and the service logs not primary and secondaryOk=false. db.hello() on mongo02 shows isWritablePrimary false and primary as mongo01.example.com:27017. The engineer replaces the string with the three-member form and replicaSet=rs0, and the service follows the primary from then on.

Confirm the fix

  1. The application logs no new not primary and secondaryOk=false after a restart.
  2. A test write succeeds through the application.
  3. Run rs.status() again after a planned step-down in a quiet period. The application continues on the new primary.
  4. For secondary reads, db.getMongo().getReadPrefMode() in a shell with the same string prints secondary.

What to watch afterwards

  • Use the host names of all members and the set name in every application string. A string with directConnection=true, or a driver that treats one host as a single server, ties the application to that member's role.
  • Keep member host names resolvable from the application hosts. A name that stops resolving removes that member from the driver's view.
  • Check read-only jobs for stale data after you point them at secondaries.
  • Record which service uses which read preference, so a step-down test shows what changes.

Risks and limits

  • These commands have not been run or lab-tested.
  • Wording and error codes are from the MongoDB source code at release r8.0.38. Other releases may print other wording.
  • This covers replica sets. Sharded clusters route through mongos and are not described here.
  • Reading from a secondary never gives the guarantees a primary read gives. Do not point write paths or read-your-write logic at a secondary.

Sources