Search code examples
mongodbreplicasetmongodb-replica-set

What's the equivalent of `rs.slaveOk()` for config file in MongoDB?


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?


Solution

  • 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.