1

I have set up my MongoDB on local machine with replicaSet, and I have this error:

An error occurred while loading navigation: 'not master and slaveOk=false': It is recommended to change your read preference in the connection dialog to Primary Preferred or Secondary Preferred or provide a replica set name for a full topology connection.

If I don't exec this command on secondary servers,

rs.slaveOk()

After using the command, the problem is solved, but only temporarily. After restarting the servers, the above error pops up again, I again have to stop it using the command.

How can I somehow define the slaveOk in a config file, so that I don't have to allow slaveOk each time I start the server?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
ezio4df
  • 3,541
  • 6
  • 16
  • 31
  • Because you are connecting to secondary specifically, and it is trying to stop you unintentionally making changes to secondary which will not be replicated to any other node. – Yahya Jul 24 '20 at 10:21
  • So, how am i supposed to do it ?? (i am using on nodejs) – ezio4df Jul 24 '20 at 10:23

1 Answers1

3

Your connection string should look something like this:

mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl

If you are connecting from shell

mongo "mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl"

OR

mongo --host myRepl/mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017

Where myRepl is the replica set name, mongodbX.example.com.local:27017 are your nodes.

Yahya
  • 3,386
  • 3
  • 22
  • 40