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

前端 未结 8 1427
遥遥无期
遥遥无期 2020-11-30 17:24

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 p

相关标签:
8条回答
  • 2020-11-30 17:45

    THIS IS JUST A NOTE FOR ANYONE DEALING WITH THIS PROBLEM USING THE RUBY DRIVER

    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 = MongoClient.new("localhost", 27017, { slave_ok: true })
    

    https://github.com/mongodb/mongo-ruby-driver/wiki/Tutorial#making-a-connection

    mongo_client = MongoClient.new # (optional host/port args)
    

    Notice that 'args' is the third optional argument.

    0 讨论(0)
  • 2020-11-30 17:46

    I got here searching for the same error, but from Node.js native driver. The answer for me was combination of answers by campeterson and Prabhat.

    The issue is that readPreference setting defaults to primary, which then somehow leads to the confusing slaveOk error. My problem is that I just wan to read from my replica set from any node. I don't even connect to it as to replicaset. I just connect to any node to read from it.

    Setting readPreference to primaryPreferred (or better to the ReadPreference.PRIMARY_PREFERRED constant) solved it for me. Just pass it as an option to MongoClient.connect() or to client.db() or to any find(), aggregate() or other function.

    • https://docs.mongodb.com/v3.0/reference/read-preference/#primaryPreferred
    • http://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html (search readPreference)
    const { MongoClient, ReadPreference } = require('mongodb');
    const client = await MongoClient.connect(MONGODB_CONNECTIONSTRING, { readPreference: ReadPreference.PRIMARY_PREFERRED });
    
    0 讨论(0)
  • 2020-11-30 17:47

    slaveOk does not work anymore. One needs to use readPreference https://docs.mongodb.com/v3.0/reference/read-preference/#primaryPreferred

    e.g.

    const client = new MongoClient(mongoURL + "?readPreference=primaryPreferred", { useUnifiedTopology: true, useNewUrlParser: true });
    
    0 讨论(0)
  • 2020-11-30 17:48

    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.

    0 讨论(0)
  • 2020-11-30 17:49

    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.

    0 讨论(0)
  • 2020-11-30 17:51

    WARNING: slaveOk() is deprecated and may be removed in the next major release. Please use secondaryOk() instead. rs.secondaryOk()

    0 讨论(0)
提交回复
热议问题