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:

rs.slaveOk()

Or just reconfigure your mongo driver.

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:

      http://blog.mongolab.com/2014/02/mongodb-driver-tips-tricks-mongoid-3/#By_default_Mongoid_reads_from_secondaries

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

      http://docs.mongodb.org/manual/core/read-preference/#read-preference-modes

      This here is an older post, but it might be interesting for you:

      https://groups.google.com/forum/#!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:

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s