MongoDB PHP:从从从从服务器读取数据,并使用繁重的读取环境设置持久连接

时间:2022-08-24 03:47:48

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

我正在尝试设置所有传入的读取查询来攻击mongo服务器上的奴隶。

I see in the PHP docs a reference to:

我在PHP文档中看到:

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:

PHP文档向我展示了这个示例:

$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 个解决方案

#1


7  

SlaveOkay and Read Preferences

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

slaveok首选项实际上是“次要首选项”,但仍然允许从主首选项进行读取。

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

MongoDB 2.2和Mongo PHP 1.3.0驱动程序引入了几种新的读偏好模式,所以现在有:

  • primary - only read from the primary
  • 主-只读从主。
  • primaryPreferred - read from the primary unless it is unavailable
  • primary preferred - read from the primary, unless it is unavailable
  • secondary - only read from secondaries
  • 二级-只阅读二级。
  • secondaryPreferred - prefer reads from a secondary (equivalent semantics to slaveOK)
  • 二级首选——更喜欢从二级读取(与slaveOK等价的语义)
  • nearest - read from the nearest member of the replica set (by ping time)
  • 最近的—从副本集中最近的成员(按ping时间)读取

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' }.

MongoDB 2.2中的另一个新特性是对标记集的支持,它允许您通过标记复制集成员来指定自定义的读取首选项。这使您可以针对特定的次要目标。例如,一个复制集成员可以被标记为:{'group': 'reporting'}。

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

有关PHP驱动程序中的读首选项的更多信息,请参阅PHP.net上的Mongo手册:读首选项。

Persistent Connections

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

PHP 1.3.0驱动版本重写了连接池,现在所有连接都是持久的。

Per the changelog:

每更新日志:

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

删除了“persist”选项,因为所有连接现在都是持久化的。它仍然可以使用,但不会影响任何东西。

#1


7  

SlaveOkay and Read Preferences

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

slaveok首选项实际上是“次要首选项”,但仍然允许从主首选项进行读取。

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

MongoDB 2.2和Mongo PHP 1.3.0驱动程序引入了几种新的读偏好模式,所以现在有:

  • primary - only read from the primary
  • 主-只读从主。
  • primaryPreferred - read from the primary unless it is unavailable
  • primary preferred - read from the primary, unless it is unavailable
  • secondary - only read from secondaries
  • 二级-只阅读二级。
  • secondaryPreferred - prefer reads from a secondary (equivalent semantics to slaveOK)
  • 二级首选——更喜欢从二级读取(与slaveOK等价的语义)
  • nearest - read from the nearest member of the replica set (by ping time)
  • 最近的—从副本集中最近的成员(按ping时间)读取

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' }.

MongoDB 2.2中的另一个新特性是对标记集的支持,它允许您通过标记复制集成员来指定自定义的读取首选项。这使您可以针对特定的次要目标。例如,一个复制集成员可以被标记为:{'group': 'reporting'}。

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

有关PHP驱动程序中的读首选项的更多信息,请参阅PHP.net上的Mongo手册:读首选项。

Persistent Connections

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

PHP 1.3.0驱动版本重写了连接池,现在所有连接都是持久的。

Per the changelog:

每更新日志:

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

删除了“persist”选项,因为所有连接现在都是持久化的。它仍然可以使用,但不会影响任何东西。