使用副本集时,不同服务器应该连哪个 mongodb?

2015-04-28 20:30:20 +08:00
 laike9m

副本集有三台机器(M1, M2, M3),然后服务器放在另外几台机器(S1, S2, ..Sn)。用的 driver 是 pymongo。因为对实时性要求不高,所以 ReadPreference 打算设置成 NEAREST,也就是会把请求平均分布到各台 mongo 所在机器。然后现在的问题是,MongoReplicaSetClient 的第一个参数 hosts_or_uri 不知道该怎么设置。可能的选择有三种:

  1. 在各台后端服务S1-Sn,都设置 hosts_or_uri = M1;
  2. 平均分配, 1/3 hosts_or_uri = M1, 1/3 hosts_or_uri = M3, 1/3 hosts_or_uri = M3;
  3. 设置 hosts_or_uri = M1, M2, M3

哪一种比较好呢?谢谢。

5040 次点击
所在节点    MongoDB
5 条回复
whatisnew
2015-04-28 22:50:18 +08:00
关于 mongodb 我一直没学会,如何删除前30行数据。

选择数据 asc 排序,删除前 asc 30行。试过 remove findandmodify 全都没成功,半小时后放弃。。。
EPr2hh6LADQWqRVH
2015-04-28 23:04:35 +08:00
读可以用slave,写可不行,但repl set的语境下对于任意时间点到底谁是Master这个问题是不确定的,所以任意一台需要访问mongodb的应用服务器都需要了解所有可能是Master的mongod实例地址。
具体URI格式参考官方文档 http://docs.mongodb.org/manual/reference/connection-string/
laike9m
2015-04-28 23:46:06 +08:00
@avastms 嗯,我知道写不行,写的话会练到master的。如果只考虑度,一般来说初始化client的时候应该把所有地址都写上么?
laike9m
2015-04-29 11:08:16 +08:00
mongodb 官方的人给了个回复:

You're only required to pass the hostname/port of one of the replica set members. MongoReplicaSetClient will discover the rest automatically. The being said, it is best practice to include more than one member in the seed list (hosts_or_uri). If you only include one member in the seed list and that member just happens to be unavailable when you (re)start your application, PyMongo won't be able to connect. If you include two or more members in the seed list, as long as one of the members in the seed list is available PyMongo will be able to connect.
laike9m
2015-04-29 11:08:54 +08:00
所以最好是把所有 Mongodb 所在机器都写上。性能上应该没区别。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/187075

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX