8

我正在开发一个使用 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
4

1 回答 1

6

我建议您添加日志记录以了解发生了什么。Mongo 驱动程序(从 1.3 版开始)提供了广泛的日志记录来调试连接问题。在http://derickrethans.nl/mongodb-debugging.html有一篇文章解释了如何开启它。如果从日志信息中不清楚会发生什么,请使用日志更新您的问题,以便我可以更新我的答案。

从发布的日志中,我可以看到您的客户端无法从您的连接字符串连接到任何种子。如果您的机器/客户端无法连接,您当然无法与 MongoDB 对话。在这种情况下,可能有几种可能性:

  • 驱动程序无法将名称转换为 IP 地址。在这种情况下,您需要修复 DNS 设置,或将所有主机添加到 /etc/hosts
  • 您真的无法连接到它们,这可能是防火墙。
  • 1.2系列的驱动存在一些问题,所以如果你还没有使用1.3.4或更高版本,我建议你升级。这应该是一个更流畅的体验。
  • 请记住,您需要大多数节点才能进行写入。如果您没有,那么您将收到“无候选服务器”错误。
于 2013-01-30T11:57:03.040 回答