我正在使用MongoDB复制集开发一个PHP Web应用程序来存储我的数据。我偶尔会收到以下错误:
致命错误:未捕获的异常' MongoConnectionException'有消息'没有找到候选服务器'
我有一个3成员的Mongo副本集,有1个仲裁者
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
}
以下是我偶尔会收到此错误的代码示例。
$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']);
任何人都知道可能导致找不到候选服务器的原因是什么?关于如何确定问题根源的消息或任何建议?
我的所有mongo.conf文件都使用每台服务器的静态IP,没有防火墙和DNS似乎正在正确解析到我网络上的.local域。
编辑:在实例化MongoClient之前,我添加了Derick建议的日志记录。
error_reporting (E_NOTICE);
MongoLog::setModule( MongoLog::ALL );
MongoLog::setLevel( MongoLog::ALL );
启用日志记录后,我收到以下通知。
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
答案 0 :(得分:6)
我建议你添加日志以了解发生了什么。 Mongo驱动程序(自1.3版本开始)提供扩展日志记录来调试连接问题。 http://derickrethans.nl/mongodb-debugging.html上有一篇文章解释如何启用它。如果从日志信息中不清楚会发生什么,请使用日志更新您的问题,以便我可以更新我的答案。
从发布的日志中我可以看到您的客户端无法连接到连接字符串中任何种子。如果您的机器/客户端无法连接,您当然无法与MongoDB通信。在这种情况下,可能存在一些可能性: