dtrhd2850 2012-11-20 14:50 采纳率: 0%
浏览 71
已采纳

MongoDB PHP:从Slaves读取并在繁重的读取环境中设置持久连接

I'm attempting to set all incoming read queries to hit slaves on my mongo servers.

I see in the PHP docs a reference to:

MongoCursor::$slaveOkay = true;

However, this just seems to setup queries to be fired off to slaves; not really to do anything else. My connections to my servers look like this:

$mongo = new Mongo("mongodb://my.server:27017", 
                      array("replicaSet" => 'replicaSet', "persist" => "pool")
                  );
  • Will I need to do anything different with my persist connection when wanting to only connect to the slave for reads?

  • How can I target queries to hit just the Slave so that the writes I have on the primary won't block incoming read requests.

PHP docs shows me this example:

$db->setSlaveOkay(true);
$c = $db->myCollection;

$cursor = $c->find();

However I'm confused at the difference this has between the above, and if both are needed or not.

  • 写回答

1条回答 默认 最新

  • dongmen1860 2012-11-20 19:39
    关注

    SlaveOkay and Read Preferences

    The SlaveOkay preference is effectively "secondary preferred", but still allows reading from the primary.

    MongoDB 2.2 and the Mongo PHP 1.3.0 driver introduce several new Read Preference Modes so there is now:

    • primary - only read from the primary
    • primaryPreferred - read from the primary unless it is unavailable
    • secondary - only read from secondaries
    • secondaryPreferred - prefer reads from a secondary (equivalent semantics to slaveOK)
    • nearest - read from the nearest member of the replica set (by ping time)

    Another new feature in MongoDB 2.2 is support for Tag Sets, which allow you to specify custom read preferences by tagging replica set members. This allows you to target specific secondaries. For example, a replica set member could be tagged with: { 'group' : 'reporting' }.

    For more information on read preferences in the PHP driver see the Mongo manual on PHP.net: Read Preferences.

    Persistent Connections

    Connection pooling has been rewritten for the PHP 1.3.0 driver release, and all connections are now persistent.

    Per the changelog:

    Removed the "persist" option, as all connections are now persistent. It can still be used, but it doesn't affect anything.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测