Saturday, June 16, 2012

mongo , replica set error: { "$err" : "not master and slaveok=false", "code" : 13435 }

Hi,
I am using Ubuntu machine and i started three instance of mongod with replica set.
On primary instance i was able to see the db and collections and data.

But on secondary when i did query for find , i got this "error: { "$err" : "not master and slaveok=false", "code" : 13435 }".

so solution of this is , you have to run the rs.slaveOk() before quering to mongo shell know that you're allowing reads from a secondary.

Ex.
$ mongo hostAddress:ipAddress
>use databaseName;
> rs.slaveOk();
>db.collectionName.find();


Any doubt , Please share with me.



4 comments:

  1. Hi,
    Every Session we have to execute rs.slaveOk() to allowing reads from a secondary..

    How to fix this without executing rs.slaveOk() to allow reads from a secondary.

    Thanks,
    Kishore E.

    ReplyDelete
  2. Hi Kishore,
    We can use below shorthand for this.
    db.getMongo.setSlaveOk()

    The current connection to allow read operations to run on secondary members.

    ReplyDelete
  3. Hi,

    kish:SECONDARY> db.getMongo.setSlaveOk()
    2017-05-12T02:39:55.073-0400 E QUERY [thread1] TypeError: db.getMongo.setSlaveOk is not a function :

    Please help me on this...

    ReplyDelete
    Replies
    1. This below command is used when we create mongo connection using javascript and need to run query on slave
      db.getMongo().setSlaveOk()

      Ok, so you need to do this using console.
      Check this file .mongorc.js in your home directory and add rs.slaveOk() there.
      The file is being evaluated on each shell startup.
      For detail check here.

      Delete