何时查询 Mongo DB 的副本集

发布于 2024-12-11 16:08:07 字数 694 浏览 0 评论 0原文

我很难理解的一件事是,在设置当前的 Mongo 配置时,我们目前有两台带有仲裁器的服务器。

现在,我首先在 master 上创建了一条记录;去了辅助然后试图找到它,我收到了如下错误:

error: { "$err" : "not master and slaveok=false", "code" : 13435 }

在 mongo 网站上阅读有关 Slave OK 我发现在辅助服务器上我需要设置

rs.slaveOk();

但是我不完全理解为什么在查询服务器时我会在 PHP 中这样做;或者如果我完全误解了这一点。

本质上,我有一个服务器池,我计划这样连接到它们:

$m = new Mongo("mongodb://localhost:27017", array("replicaSet" => "myReplSetName"));?>

然后这将连接到主服务器..但这似乎并没有分配读取负载..我怎样才能分配读取负载它会跨越两台服务器从而使查询更快吗?

先感谢您

One thing I'm struggling to understand is when setting up our current Mongo configuration we have two servers with an arbitrator currently.

Now first off I created a record on the master; went to the secondary then tried to find it and I was receiving errors which looked like:

error: { "$err" : "not master and slaveok=false", "code" : 13435 }

Upon reading on mongo's website about Slave OK I found that on the secondary servers I needed to set

rs.slaveOk();

However I'm not totally understanding why I would do this in PHP when querying the servers; or if I'm mis-understanding the point all together.

Essentially I have a pool of servers, and I was planning on connecting to them as such:

$m = new Mongo("mongodb://localhost:27017", array("replicaSet" => "myReplSetName"));?>

This would then connect to the master.. however this doesn't appear to distribute the read load then.. how could I distribute the read load so that it would span across both servers and thus make queries quicker?

Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱格式化 2024-12-18 16:08:07

这是连接到副本集的可接受的方式。假设指定的服务器已启动,驱动程序连接将发现集群中的其余服务器,并能够在允许时将查询定向到它们,并通过故障转移事件遵循“PRIMARY”指定。不过,最好将其他服务器添加到此“种子列表”中,以便在其中一台“种子”服务器发生故障时连接仍然可以成功。

至于分发读取......这种行为实际上是由驱动程序自行决定的。但普遍接受的语义是:如果请求slaveOk读取,则该读取查询将转到SECONDARY服务器(如果有);只有在没有可用的情况下才会进入 PRIMARY。当然,写查询总是会去PRIMARY。

让 PRIMARY 分担一些读取负载的一个建议是,当您将查询标记为 SlaveOk 时进行随机化 - 请参阅此处的讨论

要在 PHP 中将查询标记为 SlaveOk,您可以在多个级别设置此位 - 连接、数据库、集合或集群 -- API 详细信息位于此处 这提供了几种可能的方式实现上述随机化技术的方法——例如,尝试确保连接池中的一半连接设置了slaveOk 位。

希望有帮助!

That's an acceptable way to connect to the replica set. Assuming that the named server is up, the driver connection will discover the remainder of the servers in the cluster and be able to direct queries to them when permitted, and follow the "PRIMARY" designation through failover events. It's a good idea to add other servers to this "seed list", though, so that the connection can still succeed if one of the "seed" servers happens to be down.

As for distributing reads ... this behavior is in practice at the discretion of the driver. But the generally accepted semantics is: if a slaveOk read is requested, that read query will go to a SECONDARY server if one is available; only if none are available will it go to the PRIMARY. Write queries, of course, will always go to the PRIMARY.

One suggestion for having the PRIMARY share some of the read load, is to randomize when you flag a query as slaveOk -- see the discussion here

To flag a query as slaveOk in PHP, you can set this bit at several levels -- connection, database, collection, or cluster -- API details here This affords several possible ways of implementing the aforementioned randomization technique -- e.g., try to make sure half the connections in your connection pool have the slaveOk bit set.

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文