使用PHP客户端Mongodb复制,避免节点之间的连接

时间:2015-05-20 08:59:40

标签: php mongodb replicaset

我们有一个带有2个节点的Mongodb复制集,每个节点位于不同国家的不同CPD中。

两个节点之间的延迟是可测量的。这在写入数据时不是问题,因为这是每天使用离线过程完成的一次。

但是在阅读时,在每个请求中,节点会在它们之间建立连接,这需要花费大量时间并使请求变慢,并在网络上产生问题。

我们发现主节点和辅助节点之间有大约700个连接。

我尝试了几种连接,连接字符串和readPreference设置,但总会发生这种情况。

有没有办法在每个请求的节点之间没有连接?当节点之间的距离不是很近时,人们如何应对这个问题呢?

修改

如果我重复运行测试脚本,有时会非常快。我在MongoLog详细信息中看到了这一点:

180 [+0.004990] IO FINE getting cursor body
181 [+0.005032] CON FINE No timeout changes for xxxx:27017;rs1;.;101877
182 [+0.005080] CON FINE Initializing cursor timeout to 30000 (from connection options)
183 [+0.005095] CON INFO command supports Read Preferences
184 [+0.005109] CON INFO mongo_get_read_write_connection: finding a REPLSET connection (read)
185 [+0.005128] CON FINE found connection xxxxx:27017;rs1;.;101877 (looking for xxxx:27017;rs1;.;101877)
186 [+0.005140] CON FINE is_ping: skipping: last ran at 1432113918, now: 1432113922, time left: 1
187 [+0.005153] CON FINE found connection yyyy:27017;rs1;.;101877 (looking for yyyyy:27017;rs1;.;101877)
188 [+0.005163] CON FINE is_ping: skipping: last ran at 1432113919, now: 1432113922, time left: 2
189 [+0.005175] CON FINE discover_topology: checking ismaster for xxxxx:27017;rs1;.;101877
190 [+0.005186] CON FINE found connection xxxx:27017;rs1;.;101877 (looking for xxxxx:27017;rs1;.;101877)
191 [+0.005196] CON FINE ismaster: skipping: last ran at 1432113919, now: 1432113922, time left: 12
192 [+0.005206] CON FINE discover_topology: ismaster got skipped
193 [+0.005218] CON FINE discover_topology: checking ismaster for yyyyy:27017;rs1;.;101877
194 [+0.005228] CON FINE found connection yyyyy:27017;rs1;.;101877 (looking for yyyyy:27017;rs1;.;101877)
195 [+0.005237] CON FINE ismaster: skipping: last ran at 1432113919, now: 1432113922, time left: 12
196 [+0.005246] CON FINE discover_topology: ismaster got skipped

当3个定时器中的任何一个达到0时,就会建立连接。看起来我可以增加3次检查的间隔,对吗?

1 个答案:

答案 0 :(得分:0)

我最终通过做这些改变来解决问题:

  • mongo.is_master_intervalmongo.ping_interval设置更改为更高的值
  • 更改读取和写入操作的连接设置。为了阅读,我使用独立的连接配置(没有replicaSet参数),并且用于写入replicaSet配置。这样,读取操作就不会在服务器之间产生连接。