使用 MAMP 无法访问 MongoDB 副本集

Unreachable MongoDB replica set using MAMP

我在 AWS EC2 上 运行 三个远程实例副本集,并在端口 27017 和 ICMP 上为我的 IP 打开了安全组(防火墙)。

我的开发环境是 PHP 5.6.2 on Mac OS X 的 MAMP。 在 PHP 测试文件中新建 MongoClient 时,我提供一个连接字符串:

mongodb://52.68.25.131:27017,52.68.29.231:27017,52.68.50.99:27017/?replicaSet=rs0

错误显示:

MongoConnectionException: No candidate servers found Caused by MongoConnectionException: MongoClient::__construct(): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known

仍然无法连接到副本集。以下是我的副本集的状态。


rs0:PRIMARY> rs.status()

{
    "set": "rs0",
    "date": ISODate("2015-04-09T12:24:15.195Z"),
    "myState": 1,
    "members": [
    {
        "_id": 0,
        "name": "ip-172-30-0-126:27017",
        "health": 1,
        "state": 1,
        "stateStr": "PRIMARY",
        "uptime": 4714,
        "optime": Timestamp(1428578838,
        1),
        "optimeDate": ISODate("2015-04-09T11:27:18Z"),
        "electionTime": Timestamp(1428578787,
        1),
        "electionDate": ISODate("2015-04-09T11:26:27Z"),
        "configVersion": 3,
        "self": true
    },
    {
        "_id": 1,
        "name": "172.30.2.133:27017",
        "health": 1,
        "state": 2,
        "stateStr": "SECONDARY",
        "uptime": 3467,
        "optime": Timestamp(1428578838,
        1),
        "optimeDate": ISODate("2015-04-09T11:27:18Z"),
        "lastHeartbeat": ISODate("2015-04-09T12:24:14.067Z"),
        "lastHeartbeatRecv": ISODate("2015-04-09T12:24:14.821Z"),
        "pingMs": 2,
        "syncingTo": "ip-172-30-0-126:27017",
        "configVersion": 3
    },
    {
        "_id": 2,
        "name": "172.30.2.132:27017",
        "health": 1,
        "state": 2,
        "stateStr": "SECONDARY",
        "uptime": 3414,
        "optime": Timestamp(1428578838,
        1),
        "optimeDate": ISODate("2015-04-09T11:27:18Z"),
        "lastHeartbeat": ISODate("2015-04-09T12:24:13.821Z"),
        "lastHeartbeatRecv": ISODate("2015-04-09T12:24:13.818Z"),
        "pingMs": 2,
        "syncingTo": "172.30.2.133:27017",
        "configVersion": 3
    }
    ],
    "ok": 1

在为所有三个实例分配 EIP(弹性 IP)后获得三个 public DNS 并将它们用作副本集的新配置。

从主节点 rs.reconfig(<new-setting>) 之后,它就像一个魅力。