我正在使用MongoDB复制集开发一个
PHP Web应用程序来存储我的数据.我偶尔会收到以下错误:
Fatal error: Uncaught exception ‘MongoConnectionException’ with message ‘No candidate servers found’
我有一个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","state" : 2,"stateStr" : "SECONDARY","uptime" : 10720,"lastHeartbeat" : ISODate("2013-01-30T01:04:04Z"),"pingMs" : 3 },{ "_id" : 2,"name" : "ericsmacbookair.local:27017","state" : 7,"stateStr" : "ARBITER","uptime" : 1206,"lastHeartbeat" : ISODate("2013-01-30T01:04:03Z"),"pingMs" : 4 },{ "_id" : 3,"name" : "ericsmacxps.local:27017","uptime" : 75204,"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,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有一篇文章解释如何打开它.如果从日志信息中不清楚会发生什么,请使用日志更新您的问题,以便我可以更新我的答案.
从发布的日志中我可以看到您的客户端无法连接到您的连接字符串中的任何种子.如果您的机器/客户端无法连接,您当然无法与MongoDB通信.在这种情况下,可能有几种可能性:
>驱动程序无法将名称转换为IP地址.在这种情况下,您需要修复DNS设置,或将所有主机添加到/ etc / hosts>你真的无法连接到它们,这可能是防火墙.>在1.2系列驱动程序中存在一些问题,所以如果你还没有使用1.3.4或更高版本,我建议你升级.这应该是一个更顺畅的经历.>请记住,为了能够编写,您需要大多数节点.如果您没有,那么您将收到“无候选服务器”错误.