Search code examples
mongodbsharding

Is there a way to enable setSlaveOkay in the entire cluster


I have a sharded cluster which has a replica set sharded, my application layer is talking to mongos , and now if my primary server goes down, my php application throws an error.
I know this is because I have not enabled setSlaveOkay from the driver by doing something like this:

MongoCursor::$slaveOkay = true;

The problem with this is, where ever I am talking to db I have to add this statement.

Is there a way that I could enable this in the system level?

I tried doing rs.slaveok() in my primary and secondary both but still my application layer is throwing an error .

I also tried db.getMongo().setSlaveOk() in mongos console

The error statement is as follows:

PHP Fatal error:  Uncaught exception 'MongoCursorException' with message 'dbclient error communicating with server: testserver:10001'

Solution

  • You can set slaveOK at several levels in PHP: connection, database, collection, or cursor.

    The various details are to be found here:

    http://php.net/manual/en/mongo.queries.php

    You are setting it for each cursor, which is why you are having to call it so often. Instead you can set this at the db or collection level. In terms of what is happening behind the scenes - slaveOK is actually set for each connection - PHP is just managing that for you when you set it for the database, collection, cursor etc.

    Setting it at the mongos or anywhere else in the shell simply sets it for that connection. Once you log out, it is no longer set.