tsadkan yitbarek
tsadkan yitbarek

Reputation: 1370

MongoDB error not master and slaveOk=false

I am using MongoDB with Loopback in my application with a loopback connector to the MongoDB. My application was working fine but now it throws an error

not master and slaveOk=false.

Upvotes: 2

Views: 6797

Answers (2)

Greg Wozniak
Greg Wozniak

Reputation: 7172

You are attempting to connect to secondary replica whilst previously your app (connection) was set to connect likely to the primary, hence the error. If you use rs.secondaryOk() (slaveOk is deprecated now) you will possibly solve the connection problem but it might not be what you want.

To make sure you are doing the right thing, think if you want to connect to the secondary replica instead of primary. Usually, it's not what you want.

If you have permissions to amend the replica configuration

I suggest to connect using MongoDB Compass and execute rs.status() first to see the existing state and configuration for the cluster. Then, verify which replica is primary.

If necessary, adjust priorities in the replicaset configuration to assign primary status to the right replica. The highest priority number sets the replica as primary. This article shows how to do it right.

If you aren't able to change the replica configuration

Try a few things:

  • make sure your hostname points to the primary replica
  • if it is a local environment issue - make sure you added your local replica hostnames to the /etc/hosts pointing to 127.0.0.1
  • experiment with directConnection=true
  • experiment with multiple replica hosts and ?replicaSet=<name> - read this article (switch tabs to replica)

The best bet is that your database configuration has changed and your connection string no longer reflects it correctly. Usually, slight adjustments in the connection string are needed or just checking to what instance you want to connect.

Upvotes: 0

Mike_Jr
Mike_Jr

Reputation: 345

try running rs.slaveOk() in a mongoDB shell

Upvotes: 4

Related Questions