从Slave获取MongoDb主节点

时间:2014-10-10 19:48:33

标签: mongodb

在主/从mongodb设置中,无论如何都要向从节点询问它的主节点?现在,我只知道Mongo slave的dns名称,但我希望能够找出主节点的dns名称。

1 个答案:

答案 0 :(得分:1)

您可以尝试在奴隶上运行rs.status();。它将返回所有节点的信息。

示例输出:

"members" : [
    {
        "_id" : 0,
        "name" : "m1.example.net:27017",
        "health" : 1,
        "state" : 1,
        "stateStr" : "PRIMARY",
        "uptime" : 269,
        "optime" : Timestamp(1404225575, 11),
        "optimeDate" : ISODate("2014-05-01T14:39:35Z"),
        "electionTime" : Timestamp(1404225586, 1),
        "electionDate" : ISODate("2014-05-01T14:39:46Z"),
        "self" : true
    },
    {
        "_id" : 1,
        "name" : "m2.example.net:27017",
        "health" : 1,
        "state" : 2,
        "stateStr" : "SECONDARY",
        "uptime" : 265,
        "optime" : Timestamp(1404225575, 11),
        "optimeDate" : ISODate("2014-05-01T14:39:35Z"),
        "lastHeartbeat" : ISODate("2014-05-01T14:44:03Z"),
        "lastHeartbeatRecv" : ISODate("2014-05-01T14:44:02Z"),
        "pingMs" : 0,
        "syncingTo" : "m1.example.net:27017"
    },
    {
        "_id" : 2,
        "name" : "m3.example.net:27017",
        "health" : 1,
        "state" : 2,
        "stateStr" : "SECONDARY",
        "uptime" : 265,
        "optime" : Timestamp(1404225575, 11),
        "optimeDate" : ISODate("2014-05-01T14:39:35Z"),
        "lastHeartbeat" : ISODate("2014-05-01T14:44:02Z"),
        "lastHeartbeatRecv" : ISODate("2014-05-01T14:44:02Z"),
        "pingMs" : 0,
        "syncingTo" : "m1.example.net:27017"
    }
]