通过已删除的成员连接到副本集成员

时间:2019-04-17 13:17:06

标签: database mongodb replication replicaset

我有一个包含3个成员的简单副本集

rs.status()

的输入
{
    ...,
    "members" : [
        {
            "_id": 1,
            "name": "localhost:27021",
            "stateStr": "PRIMARY",
        },
        {
            "_id": 2,
            "name": "localhost:27022",
            "stateStr": "SECONDARY",
        },
        {
            "_id": 3,
            "name": "localhost:27023",
            "stateStr": "SECONDARY",
        }
    ]
}

我删除了一名成员

rs.remove("localhost:27023")

现在 rs.status()

{
    ...,
    "members" : [
        {
            "_id": 1,
            "name": "localhost:27021",
            "stateStr": "PRIMARY",
        },
        {
            "_id": 2,
            "name": "localhost:27022",
            "stateStr": "SECONDARY",
        },
    ]
}

现在,我将mongodb连接URL用于已删除的实例。它成功连接到副本集成员!!

var url = 'mongodb://localhost:27023/myproject?replicaSet=rs0';
MongoClient.connect(url, function(err, db) {
  console.log("Connected correctly to server");
  db.close();
});

我可以看到该连接是使用副本集成员(localhost:27022和localhost:27023)建立的

2019-04-17T18:59:04.727+0900 I NETWORK  [thread1] connection accepted from 127.0.0.1:53284 #6 (3 connections now open)
2019-04-17T18:59:04.727+0900 I NETWORK  [conn6] received client metadata from 127.0.0.1:53284 conn6: { driver: { name: "nodejs", version: "3.2.3" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.15.0-47-generic" }, platform: "Node.js v10.15.0, LE, mongodb-core: 3.2.3" }
2019-04-17T18:59:04.736+0900 I -        [conn6] end connection 127.0.0.1:53284 (3 connections now open)
2019-04-17T18:59:35.334+0900 I ASIO     [NetworkInterfaceASIO-RS-0] Ending idle connection to host localhost:27021 because the pool meets constraints; 1 connections to that host remain open

有人可以解释吗? 通过已删除的成员连接到副本集成员。 在任何文档中都找不到任何解释。

1 个答案:

答案 0 :(得分:3)

这是驱动程序行为。默认情况下,客户端连接到副本集将尝试连接到主节点。在这种情况下,连接到mongodb://localhost:27023/myproject?replicaSet=rs0将首先连接到localhost:27023,并使用命令{isMaster: 1}检查连接的节点是主节点还是主节点。从结果中,它检测到localhost:27023不是主节点/主节点,但它还有其他可用节点信息。下一步是连接到其他节点,直到找到主要节点为止。

您应该关闭localhost:27023,它甚至都不会连接。