How to set rs.slaveOk() in secondary mongodb servers in replicaset via commandline?

Create a file /etc/mongorc.js and add rs.slaveOk() there. The file is being evaluated on each shell startup.

For more information have a look here


Calling the below should work fine, there is no return type for the method so nothing will get printed back to the screen

${MONGO_HOME}/bin/mongo --port ${MONGO_PORT2} --host ${MONGO_SECONDARY2} --eval "rs.slaveOk()"

Running rs.slaveOk in the mongo.exe will also how how it is implemented as it is just a helper method:

> rs.slaveOk
function (value) { return db.getMongo().setSlaveOk(value); }
>

And also the setSlaveOk method:

> db.getMongo().setSlaveOk
function ( value ) {
     if( value == undefined ) value = true;
     this.slaveOk = value;
}

You could always try to query one of the collections on the secondary to make sure the node is queryable:

> db.test.findOne()
null 

Update - bit more clarity

Setting slaveOk() is only valid for that console session that it was executed in, so you would need to pass in a script or stay connected to the console with the --shell arguments for exmaple

C:\mongodb\bin>mongo.exe --port 27012 --eval "rs.slaveOk()" --shell
MongoDB shell version: 3.0.5
connecting to: 127.0.0.1:27012/test
type "help" for help
rs1:SECONDARY> db.test.find()
{ "_id" : ObjectId("5630fdf2af4abd9f8ae7f79c"), "test" : true }
rs1:SECONDARY>

If we don't pass in the rs.slaveOk() the we get the following response:

C:\mongodb\bin>mongo.exe --port 27012 --shell
MongoDB shell version: 3.0.5
connecting to: 127.0.0.1:27012/test
type "help" for help
rs1:SECONDARY> db.test.find()
Error: error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
rs1:SECONDARY> exit
bye