【问题标题】:PHP MongoClient "No candidate servers found" with replica set带有副本集的PHP MongoClient“未找到候选服务器”
【发布时间】:2014-04-23 22:30:56
【问题描述】:

规格:PHP 5.3.10、Ubuntu 12.04 64 位、Mongo 驱动程序 1.5.1、MongoDB 2.4.8

我们有一个带有两个辅助副本集的 Mongo 分片。我似乎无法让 PHP 驱动程序利用副本集进行高性能读/写。尝试使用下面提供的连接字符串进行连接时,我收到“未找到候选服务器”。

$connection = new MongoClient("mongodb://10.88.217.247:27017,10.88.217.247:27018",
                            array('username'=>"{$username}", 'password'=>"{$password}", 'replicaSet' => true));

这是 Mongo 日志发送的内容:

[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] ismaster: start
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] send_packet: read from header: 36
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] send_packet: data_size: 259
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] ismaster: can't find minWireVersion, defaulting to 0
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] ismaster: can't find maxWireVersion, defaulting to 0
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] ismaster: setting maxBsonObjectSize to 16777216
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] ismaster: setting maxMessageSizeBytes to 48000000
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] ismaster: can't find maxWriteBatchSize, defaulting to 1000
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] ismaster: set name: rs-0, ismaster: 1, secondary: 0, is_arbiter: 0
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] ismaster: the server name (mongo-0-0:27018) did not match with what we thought it'd be (10.88.218.248:27018).
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] ismaster: the replicaset name is not set, so we're using rs-0.
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] found host: mongo-0-0:27018
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] found host: mongo-0-2:27018
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] found host: mongo-0-1:27018
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] ismaster: last ran at 1398292169
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] discover_topology: ismaster worked, but we need to remove the seed host's connection
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] mongo_connection_destroy: Destroying connection object for 10.88.218.248:27018;-;admin/scal/a6612d715d1a4f9549232ec4d599bf55;12199
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] mongo_connection_destroy: Closing socket for 10.88.218.248:27018;-;admin/scal/a6612d715d1a4f9549232ec4d599bf55;12199.
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] discover_topology: ismaster worked
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] discover_topology: found new host: mongo-0-0:27018
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.2.122] connection_create: creating new connection for mongo-0-0:27018

rs.status() JSON:

 {
"set" : "rs-0",
"date" : ISODate("2014-04-24T15:13:39.000Z"),
"myState" : 1,
"members" : [ 
    {
        "_id" : 0,
        "name" : "mongo-0-0:27018",
        "health" : 1,
        "state" : 1,
        "stateStr" : "PRIMARY",
        "uptime" : 68415,
        "optime" : Timestamp(1398285029, 1),
        "optimeDate" : ISODate("2014-04-23T20:30:29.000Z"),
        "self" : true
    }, 
    {
        "_id" : 1,
        "name" : "mongo-0-1:27018",
        "health" : 1,
        "state" : 2,
        "stateStr" : "SECONDARY",
        "uptime" : 67984,
        "optime" : Timestamp(1398285029, 1),
        "optimeDate" : ISODate("2014-04-23T20:30:29.000Z"),
        "lastHeartbeat" : ISODate("2014-04-24T15:13:38.000Z"),
        "lastHeartbeatRecv" : ISODate("2014-04-24T15:13:38.000Z"),
        "pingMs" : 0,
        "syncingTo" : "mongo-0-0:27018"
    }, 
    {
        "_id" : 3,
        "name" : "mongo-0-2:27018",
        "health" : 1,
        "state" : 2,
        "stateStr" : "SECONDARY",
        "uptime" : 63419,
        "optime" : Timestamp(1398285029, 1),
        "optimeDate" : ISODate("2014-04-23T20:30:29.000Z"),
        "lastHeartbeat" : ISODate("2014-04-24T15:13:38.000Z"),
        "lastHeartbeatRecv" : ISODate("2014-04-24T15:13:38.000Z"),
        "pingMs" : 0,
        "syncingTo" : "mongo-0-0:27018"
    }
],
"ok" : 1

}

【问题讨论】:

  • 我不知道这是否重要,但你会在你的问题中编辑你的 PHP 版本和你的操作系统平台吗?对于可能涉及平台错误的问题,值得养成添加此数据的习惯,以防有用。
  • 我不知道 Mongo,但是“未设置副本集名称,所以我们使用 rs-0”是否相关?这是否意味着副本集需要进一步配置?
  • 尝试运行 rs.status() 并将结果粘贴到您的问题中
  • 刚刚发布了 rs.status() 的输出,谢谢。
  • 这不是完整的 mongolog 输出。完整输出将包含导致您遇到异常的连接选择逻辑详细信息。请粘贴完整的日志。此外,您将布尔值 true 传递给“replSet”,这不是您想要的(如日志所示)。请改为传递副本集名称。

标签: php mongodb


【解决方案1】:

设置副本集时可能需要注意的几件事:

  • 在构建副本集时,第一个成员将使用服务器的主机名命名。所以当你访问服务器时,它会访问连接字符串中的主机。相反,它从那里获取可用服务器列表,并尝试连接到该列表中的主机名。我解释了一些细节here。您也可以在mongodb c# driver tutorial 中找到信息:

要求每个 MongoDB 服务器都有一个可由客户端机器 DNS 解析的名称。每个 MongoDB 服务器通过 isMaster 命令报告其主机名,驱动程序使用此名称与服务器通信。当种子列表包含 IP 地址并且 MongoDB 服务器报告客户端计算机无法解析的主机名时,可能会出现此问题。

  • 当您访问一个副本集时,您应该提供一个带有参数replicaSet=[set name] 的副本集名称,如下所示。

    mongodb://hostname1,hostname2/dbName?replicaSet=rs0
    
  • 不要混淆来自不同副本集的服务器,因为:

如果您列出了多个服务器,其中一个是副本集的一部分而另一个不是,则连接模式是不确定的。确保您没有在连接字符串上混合服务器类型。

  • 记下你的 mongo 服务器的绑定 ip。如果我没记错的话,在某些发行版中它默认监听 127.0.0.1:27017。因此,您将永远无法从访客机器上获得连接。尝试在 /etc/mongodb.conf 中设置以下行:

    bind_ip = 0.0.0.0

回到你的问题。来自日志:

[Wed Apr 23 22:29:29 2014] [error] [client 170.171.1.123] ismaster: the replicaset name is not set, so we're using rs-0.

它告诉您提供副本集名称。它假定它是 rs-0(名称正确吗?)

[Wed Apr 23 22:29:29 2014] [error] [client 170.171.1.123] found host: mongo-0-0:27018
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.1.123] found host: mongo-0-2:27018
[Wed Apr 23 22:29:29 2014] [error] [client 170.171.1.123] found host: mongo-0-1:27018

说您的副本集中有 3 个成员。驱动程序将使用他们的名字连接到他们,这将是:

mongo-0-0:27018
mongo-0-1:27018
mongo-0-2:27018

如果您希望它们工作,请添加到您客户的 /etc/hosts

[ip of mongo] mongo-0-0:27018
[ip of mongo] mongo-0-0:27018
[ip of mongo] mongo-0-0:27018

或者,如果您想更改这些名称,请使用以下行:

var conf = rs.conf();
conf.members[0].host = "..." // your new name for member0
conf.member[1].host = "..."
conf.member[2].host = "..."
rs.reconfig(conf)

【讨论】:

  • 是的,副本集名称是正确的。我用 rs.status() 输出编辑了问题以确认这一点。
  • 因此驱动程序将尝试连接到 mongo-0-0/1/2。你确定这 3 个名字是可解析的吗?
  • 顺便说一句,在您的问题中,我注意到您正在连接到 27017。但是从 rs.status() 看来,您所有的服务器都在使用 27018。这也可能是一个问题。
  • 他们不是。我如何将 rs 配置更改为正确的 ips?
  • 我编辑了我的答案,有两种方法都适合你。
猜你喜欢
  • 2013-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
相关资源
最近更新 更多