问题描述
我正在开发一个使用MongoDB复制集存储我的数据的PHP Web应用程序.我偶尔会收到以下错误:
I am developing a PHP web application using a MongoDB replicaset to store my data. I occasionally receive the following error:
我有3个成员的Mongo副本集和1个仲裁器
I have a 3 member Mongo replica set with 1 arbiter
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2013-01-30T01:04:04Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "JenEricsMacPro.local:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 844478,
"optime" : Timestamp(1359507378000, 1),
"optimeDate" : ISODate("2013-01-30T00:56:18Z"),
"self" : true
},
{
"_id" : 1,
"name" : "ericsmacbookpro.local:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 10720,
"optime" : Timestamp(1359507378000, 1),
"optimeDate" : ISODate("2013-01-30T00:56:18Z"),
"lastHeartbeat" : ISODate("2013-01-30T01:04:04Z"),
"pingMs" : 3
},
{
"_id" : 2,
"name" : "ericsmacbookair.local:27017",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 1206,
"lastHeartbeat" : ISODate("2013-01-30T01:04:03Z"),
"pingMs" : 4
},
{
"_id" : 3,
"name" : "ericsmacxps.local:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 75204,
"optime" : Timestamp(1359507378000, 1),
"optimeDate" : ISODate("2013-01-30T00:56:18Z"),
"lastHeartbeat" : ISODate("2013-01-30T01:04:03Z"),
"pingMs" : 1
}
],
"ok" : 1
}
下面是我偶尔会收到此错误的代码示例.
Below is an example of the code where I occasionally receive this error.
$mongoConfig = array (
'uri' => 'mongodb://JenEricsMacPro.local:27017,ericsmacbookpro.local:27017,ericsmacxps.local:27017/',
'database' => 'myDatabase',
'replicaSetArray' => array('replicaSet' => 'rs0'));
$connection = new MongoClient(
$mongoConfig['uri'],
$mongoConfig['replicaSetArray']);
$connection->setReadPreference(MongoClient::RP_SECONDARY_PREFERRED);
$db = $connection->selectDB($mongoConfig['database']);
任何人都不知道是什么原因导致了找不到候选服务器"消息或有关如何确定问题根源的任何建议?
Anyone have any idea what might be causing the 'No candidate servers found' message or any suggestions on how I could determine the root of the problem?
我的所有mongo.conf文件都使用每台服务器的静态IP,似乎没有防火墙和DNS可以正确解析到我网络上的.local域.
All of my mongo.conf files are using the static IP of each server, no firewalls and DNS appears to be resolving correctly to the .local domain on my network.
在实例化MongoClient之前,我添加了Derick建议的日志记录.
I added logging, suggested by Derick, prior to instantiating MongoClient.
error_reporting (E_NOTICE);
MongoLog::setModule( MongoLog::ALL );
MongoLog::setLevel( MongoLog::ALL );
启用日志记录后,我收到以下通知.
After enabling logging I received the following notices.
Notice: PARSE INFO: Parsing mongodb://JenEricsMacPro.local:27017,ericsmacbookpro.local:27017,ericsmacxps.local:27017/ in MongoLogic.php on line 27
Notice: PARSE INFO: - Found node: JenEricsMacPro.local:27017 in MongoLogic.php on line 27
Notice: PARSE INFO: - Found node: ericsmacbookpro.local:27017 in MongoLogic.php on line 27
Notice: PARSE INFO: - Found node: ericsmacxps.local:27017 in MongoLogic.php on line 27
Notice: PARSE INFO: - Connection type: MULTIPLE in MongoLogic.php on line 27
Notice: PARSE INFO: - Found option 'replicaSet': 'rs0' in MongoLogic.php on line 27
Notice: PARSE INFO: - Switching connection type: REPLSET in MongoLogic.php on line 27
Notice: CON INFO: mongo_get_read_write_connection: finding a REPLSET connection (read) in MongoLogic.php on line 27
Notice: CON FINE: found connection JenEricsMacPro.local:27017;rs0;X;297 (looking for JenEricsMacPro.local:27017;rs0;X;297) in MongoLogic.php on line 27
Notice: CON INFO: is_ping: pinging JenEricsMacPro.local:27017;rs0;X;297 in MongoLogic.php on line 27
Notice: CON FINE: mongo_connection_destroy: Closing socket for JenEricsMacPro.local:27017;rs0;X;297. in MongoLogic.php on line 27
Notice: CON INFO: freeing connection JenEricsMacPro.local:27017;rs0;X;297 in MongoLogic.php on line 27
Notice: CON WARN: Couldn't connect to 'JenEricsMacPro.local:27017': send_package: error reading from socket: Operation timed out in MongoLogic.php on line 27
Notice: CON FINE: found connection ericsmacbookpro.local:27017;rs0;X;297 (looking for ericsmacbookpro.local:27017;rs0;X;297) in MongoLogic.php on line 27
Notice: CON INFO: is_ping: pinging ericsmacbookpro.local:27017;rs0;X;297 in MongoLogic.php on line 27
Notice: CON FINE: mongo_connection_destroy: Closing socket for ericsmacbookpro.local:27017;rs0;X;297. in MongoLogic.php on line 27
Notice: CON INFO: freeing connection ericsmacbookpro.local:27017;rs0;X;297 in MongoLogic.php on line 27
Notice: CON WARN: Couldn't connect to 'ericsmacbookpro.local:27017': send_package: error reading from socket: Operation timed out in MongoLogic.php on line 27
Notice: CON FINE: found connection ericsmacxps.local:27017;rs0;X;297 (looking for ericsmacxps.local:27017;rs0;X;297) in MongoLogic.php on line 27
Notice: CON INFO: is_ping: pinging ericsmacxps.local:27017;rs0;X;297 in MongoLogic.php on line 27
Notice: CON FINE: mongo_connection_destroy: Closing socket for ericsmacxps.local:27017;rs0;X;297. in MongoLogic.php on line 27
Notice: CON INFO: freeing connection ericsmacxps.local:27017;rs0;X;297 in MongoLogic.php on line 27
Notice: CON WARN: Couldn't connect to 'ericsmacxps.local:27017': send_package: error reading from socket: Operation timed out in MongoLogic.php on line 27
Notice: CON FINE: discover_topology: checking ismaster for JenEricsMacPro.local:27017;rs0;X;297 in MongoLogic.php on line 27
Notice: CON WARN: discover_topology: couldn't create a connection for JenEricsMacPro.local:27017;rs0;X;297 in MongoLogic.php on line 27
Notice: CON FINE: discover_topology: checking ismaster for ericsmacbookpro.local:27017;rs0;X;297 in MongoLogic.php on line 27
Notice: CON WARN: discover_topology: couldn't create a connection for ericsmacbookpro.local:27017;rs0;X;297 in MongoLogic.php on line 27
Notice: CON FINE: discover_topology: checking ismaster for ericsmacxps.local:27017;rs0;X;297 in MongoLogic.php on line 27
Notice: CON WARN: discover_topology: couldn't create a connection for ericsmacxps.local:27017;rs0;X;297 in MongoLogic.php on line 27
Notice: REPLSET FINE: finding candidate servers in MongoLogic.php on line 27
Notice: REPLSET FINE: - all servers in MongoLogic.php on line 27
Notice: REPLSET FINE: filter_connections: adding connections: in MongoLogic.php on line 27
Notice: REPLSET FINE: filter_connections: done in MongoLogic.php on line 27
Notice: REPLSET FINE: limiting to servers with same replicaset name in MongoLogic.php on line 27
Notice: REPLSET FINE: limiting to servers with same replicaset name: done in MongoLogic.php on line 27
Notice: REPLSET FINE: limiting by credentials in MongoLogic.php on line 27
Notice: REPLSET FINE: limiting by credentials: done in MongoLogic.php on line 27
推荐答案
我建议您添加日志记录以查找正在发生的情况. Mongo驱动程序(从1.3版开始)提供了广泛的日志记录,以调试连接问题.在 http://derickrethans.nl/mongodb-debugging.html 上有一篇文章介绍了如何打开它.如果从日志信息中尚不清楚会发生什么,请使用日志更新您的问题,以便我更新我的答案.
I would suggest you add logging to find out what is happening. The Mongo driver (since version 1.3) provides extensive logging to debug connection issues. There is an article at http://derickrethans.nl/mongodb-debugging.html to explain how to turn it on. If from the log info it is not clear what happens, update your question with the log, so that I can update my answer.
从发布的日志中,我可以看到您的客户端无法从您的连接字符串连接到任何种子.如果您的机器/客户端无法连接,那么您当然无法与MongoDB对话.在这种情况下,可能有几种可能性:
From the posted log I can see that your client can not connect to any of the seeds from your connection string. If your machine/client can't connect, you can't talk to MongoDB of course. In this case, there can be a few possibilities:
- 驱动程序无法将名称转换为IP地址.在这种情况下,您需要修复DNS设置,或将所有主机添加到/etc/hosts
- 您真的无法连接到它们,这可能是防火墙.
- 1.2系列驱动程序中存在一些问题,因此,如果您尚未使用1.3.4或更高版本,建议您进行升级.这应该是一个更加顺畅的体验.
- 请记住,您需要大部分节点才能进行写入.如果没有,则会出现没有候选服务器"错误.
这篇关于MongoConnectionException-找不到候选服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!