Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 553
NitheshKHP Avatar asked Oct 27 '15 10:10

NitheshKHP


People also ask

How does replica set connect to MongoDB?

To connect to a replica set deployment, specify the hostname and port numbers of each instance, separated by commas, and the replica set name as the value of the replicaSet parameter in the connection string. In the following example, the hostnames are host1 , host2 , and host3 , and the port numbers are all 27017 .

How do I change the read preference in MongoDB?

You must call Mongo. setReadPref() on the connection object before retrieving documents using that connection to use that read preference. To apply a read preference for a specific query or queries, you can apply cursor. readPref() to a cursor before iteration.

What is a MongoDB replica set?

In simple terms, MongoDB replication is the process of creating a copy of the same data set in more than one MongoDB server. This can be achieved by using a Replica Set. A replica set is a group of MongoDB instances that maintain the same data set and pertain to any mongod process.

What is read preference in MongoDB?

Read preference describes how MongoDB clients route read operations to the members of a replica set. click to enlarge. By default, an application directs its read operations to the primary member in a replica set (i.e. read preference mode "primary").


3 Answers

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()

like image 156
goral Avatar answered Oct 01 '22 17:10

goral


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
like image 29
Kevin Smith Avatar answered Oct 01 '22 16:10

Kevin Smith


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

like image 41
Immu Avatar answered Oct 01 '22 17:10

Immu