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.
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 .
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.
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.
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").
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()
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With