NitheshKHP
NitheshKHP

Reputation: 391

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

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

I tried following methods :

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

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

${MONGO_HOME}/bin/mongo --port ${MONGO_PORT2} --host ${MONGO_SECONDARY2} --eval "printjson(db.getSiblingDB('admin').getMongo().setSlaveOk())"

the command executes with undefined in the output log. I am trying to set this via the shell in primary server.

Upvotes: 11

Views: 35124

Answers (4)

shgnInc
shgnInc

Reputation: 2198

The rs.slaveOk() and rs.secondaryOk() are now deprecated (On Mongodb6.0 and above).

So, Use this instead:

db.getMongo().setReadPref('secondary')

Upvotes: 0

goral
goral

Reputation: 1265

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

From MongoDB version 4.4 onwards, you might get a warning displayed like:

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

So, please prefer using rs.secondaryOk()

Upvotes: 23

Immu
Immu

Reputation: 786

JFYI : looks like rs.slaveOk() will be deprecated soon, instead MongoDB suggest to use rs.secondaryOk()

Following is the official warning you gonna see in MongoShell:

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

Cheers

Upvotes: 1

Kevin Smith
Kevin Smith

Reputation: 14436

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

Upvotes: 6

Related Questions