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


Mongodb replicaset with PHP client, avoid connections between nodes

我们有一个Mongodb副本集,其中有2个节点,每个节点位于不同国家的不同CPD中。

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

但是在读取时,在每个请求中节点之间建立连接,这会花费相当多的时间并且使请求变慢,并且在网络上产生问题。

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

我已经尝试了几个连接,连接字符串和readPreference设置,但这总是发生。

是否有任何方法在每个请求的节点之间不进行连接?当节点之间的距离不那么近时,人们如何处理这个问题?

编辑:

如果我重复运行测试脚本,有时它会相当快。我在蒙古日志的详细信息中看到了这一点:

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次检查的间隔,对吧?

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

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