PHP MongoClient"没有找到候选服务器"与副本集

时间:2014-04-23 22:30:57

标签: php mongodb

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

我们有一个带有两个辅助副本集的shard 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

}

1 个答案:

答案 0 :(得分:2)

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

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

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

  • 访问副本集时,应提供带参数replicaSet = [set name]的replia set name,如下所示。

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

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

  • 请注意您的mongo服务器的bind 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)