amazon ec2 - mongodb, replicates and error: { "$err" : "not master and slaveOk=false", "code" : 13435 }


I tried mongo replica sets for the first time.

I am using ubuntu on ec2 and I booted up three instances. I used the private IP address of each of the instances. I picked on as the primary and below is the code.

mongo --host Private IP Address
rs.add(“Private IP Address”)
rs.addArb(“Private IP Address”)

All at this point is fine. When I go to the site I see that I have a primary, seconday, and arbitor.

Ok, now for a test.

On the primary create a database in this is the code:

use tt { a : 123 } )

on the secondary, I then do this and get the below error:
error: { "$err" : "not master and slaveOk=false", "code" : 13435 }

I am very new to mongodb and replicates but I thought that if I do something in one, it goes to the other. So, if I add a record in one, what do I have to do to replicate across machines?

All Answers
  • Translate

    You have to set "slave okay" mode to let the mongo shell know that you're allowing reads from a secondary. This is to protect you and your applications from performing eventually consistent reads by accident. You can do this in the shell with:


    After that you can query normally from secondaries.

    A note about "eventual consistency": under normal circumstances, replica set secondaries have all the same data as primaries within a second or less. Under very high load, data that you've written to the primary may take a while to replicate to the secondaries. This is known as "replica lag", and reading from a lagging secondary is known as an "eventually consistent" read, because, while the newly written data will show up at some point (barring network failures, etc), it may not be immediately available.

    Edit: You only need to set slaveok when querying from secondaries, and only once per session.

  • Translate

    To avoid typing rs.slaveOk() every time, do this:

    Create a file named replStart.js, containing one line: rs.slaveOk()

    Then include --shell replStart.js when you launch the Mongo shell. Of course, if you're connecting locally to a single instance, this doesn't save any typing.

  • Translate

    in mongodb2.0

    you should type


    in secondary mongod node

  • Translate


    I had this same problem when using the Ruby Gem.

    To set slaveOk in Ruby, you just pass it as an argument when you create the client like this:

    mongo_client ="localhost", 27017, { slave_ok: true })

    mongo_client = # (optional host/port args)

    Notice that 'args' is the third optional argument.

  • Translate

    I am just adding this answer for an awkward situation from DB provider.

    what happened in our case is the primary and secondary db shifted reversely (primary to secondary and vice versa) and we are getting the same error.

    so please check in the configuration settings for database status which may help you.

  • Translate

    slaveOk does not work anymore. One needs to use readPreference


    const client = new MongoClient(mongoURL + "?readPreference=primaryPreferred", { useUnifiedTopology: true, useNewUrlParser: true });