Set rs.slaveOk() is not working via CLI?

Hi Team,

I am trying to run the below command but the changes are reflecting on replication cluster secondary nodes.

mongo --port <secondaty_node_port> --eval “rs.slaveOk()”

Please advise on this.

Thanks and Regards,
Ramesh

Hi @Ramesh_Audireddy1,
i don’ t understand your question, but if you want to enable the read operation on the secondary members, you can try “rs.secondaryOk()”.

Regards

1 Like

For slaveOk vs secondaryOk see
https://www.mongodb.com/community/forums/t/info-rs-slave-is-deprecated-use-rs-secondaryok/108435/2?u=steevej

What do you mean by

3 Likes

Hi @Ramesh_Audireddy1,

I think you may be misunderstanding the use case for setting read preferences. You would set this read preference before running commands or queries via the same connection.

mongo --port <secondaty_node_port> --eval “rs.slaveOk()”

The outcome of this would be mongo shell attempting to connect to your secondary on localhost and the specified port, running the JavaScript code in --eval, and then exiting. Setting the read preference for the current session has no effect on future sessions.

Can you share more details on what you are trying to achieve? It sounds like perhaps you are trying to Write Scripts for the mongo Shell.

Regards,
Stennie

2 Likes

Hi @Stennie_X ,

Thank you for the update.

I am trying to automate the replication setup via Ansible and want to enable read for all the secondary nodes. As part of the automation, the above mentioned command is executed via ansible on the secondary nodes.

Please let me know if you need more details on this.

Thanks and Regards,
Ramesh

Hi @Fabio_Ramohitaj ,
Thank you for your reply. I am using the MongoDB 4.2 version. Seems the rs.secondaryok() will work from 4.5 or higher versions.

Thanks and Regards,
Ramesh

1 Like

Hi @steevej ,

I mean the read option is not activated even after running the rs.slaveOk(). Here is the command I executed on secondary nodes.
mongo --port <secondaty_node_port> --eval “rs.slaveOk()”
Thanks and Regards,
Ramesh

Read carefully, Stennie’s answer.

Allowing secondary reads is transient for the current connection only.

1 Like

Hi @Ramesh_Audireddy1,

Read preference is a client/driver option for routing requests, not a replica set configuration option.

If you want clients/drivers to use a secondary read preference, they need to specify this in their connection string or using a per-query/command option.

Regards,
Stennie

Hi @Stennie_X ,
Thank you for the details.

In that case, don’t I need to run the “rs.slaveOk()” on the secondary node?

Thanks and Regards,
Ramesh

Hi all,

rs.slaveOk() and rs.secondaryOk() are now deprecated.

Use this instead:

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

Cheers,
Maxime.