Search code examples
mongodbreplicaset

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.


Solution

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