How do I make a secondary slave as readable in MongoDB?

We can do this via shell command


But how we can do it permanently for all session via config file?

  • Are you referring to mongo shell sessions, or access via driver? – Stennie Jun 26 '15 at 2:23
  • I am referring shell session.. – Aman Aggarwal Jun 26 '15 at 16:03

For mongo shell sessions there is a concept of mongorc files which include JavaScript to execute when the mongo shell starts. You can use this feature to extend or customise the behaviour of the interactive shell.

If you want secondary reads to be allowed by default for all shell sessions you can either:

  • add the rs.slaveOk() command to the .mongorc.js in the current user's home directory:

    $ echo "rs.slaveOk()" >> ~/.mongorc.js
  • add to the global /etc/mongorc.js which is executed before the user-specific '~/.mongorc.js` (Note: requires MongoDB 2.6+):

    $ echo "rs.slaveOk()" | sudo tee -a /etc/mongorc.js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.