简体   繁体   中英

How to force slaveOK = true for all MongoDB connections

So I've recently inherited a LARGE body of php code, and it is less than optimal, here are the main problems:

  • It has a wrapper class for Mongo operations, such as returning database objects, collection objects, etc...but the wrapper class is not actually used in a great deal of places.
  • In many places in the code, there is extensive use of the deprecated class 'mongo' as opposed to the wrapper class.
  • The Mongo cluster is running on 1.8, however it is not an option to upgrade.

My task is to configure a replication cluster and load balance reads across the slaves. I put the 'setSlaveOkay(true)' code in the wrapper class, which ideally should have been all that was needed...however direct access to 'mongo' and other classes in very many places in the code will not have the correct slaveOkay values set. Bad code appears in so many places, that it would be inhibitive to go in and fix every occurrence.

So ideally what I'm looking for is either:

  • A way to configure the slave node so that slaveokay=true is implied for all connections across all platforms.
  • A static property or function to a mongo class which will cause all subsequent objects to have slaveokay=true

I understand that people here have a strong inclination to give solutions that are done "the right way", however in this case, suggestions like "upgrade to the newest mongo and use read preferences" or "create a wrapper class ..." will not be useful. I inherited the code, I inherited the mongo installation. I need a quick and dirty fix that won't break the existing projects...going into the code and changing 500 invocations of the 'mongo' class to use the wrapper is not an option - it will introduce too many potential bugs in the code - and also there is no guarantee that other coders on the team will stop circumventing the wrapper class.

Thanks for your time.

You can update to the latest PHP driver and use read preferences. This has nothing to do with the server side, unless you are using sharding, in which case you need a mongos that comes with MongoDB 2.4+. But if you're not using sharding, you don't need a 2.4 cluster as read preferences are handled on the client side.

I would guess you have your connection string configured somewhere? If that's the case, you can just add ?readPreference=secondaryPrefered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM