mongodb replicasets with read errors

If you have a ReplicaSet setup with MongoDB and you try to read from a slave node and you get this Exception here:

mongdodb, replicates and error: { “$err” : “not master and slaveok=false”, “code” : 13435 }

or this here:

uncaught exception: count failed: { "errmsg" : "not master", "ok" : 0 }

Than the problem is that you are not allowed to read from the slave. By default MongoDB disables reads from Slaves. You can solve that by executing this here on the slave in the mongo shell:


Or just reconfigure your mongo driver.

Published by Robert Reiz

CEO @ VersionEye. Passionated software developer since 1998.

2 thoughts on “mongodb replicasets with read errors

  1. You did not tell us any quality information on how to resolve the problem. If I am not able to get past the “not master” error after using rs.slaveOk() what should I do next?
    Your final line “Or just reconfigure your mongo driver.” Does not tell us anything nor does it reference me to instructions to tell me how to reconfigure the mongo driver.

    1. Not sure which language and which driver you are using. I’m using MongoID for Ruby. By default the driver reads from the slaves. Here is more material to that:

      Here are the read preferences you can configure for the driver:

      This here is an older post, but it might be interesting for you:!topic/mongodb-user/-5u00yC7xm4

      For me “rs.slaveOk()” solved the problem. If it doesn’t solve the problem for you, you have to check your server logs. Maybe your problem has a different root.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: