我有一个简单的副本集,有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
有人可以解释一下吗?通过已删除的成员连接到副本集成员。在任何文档中都找不到任何解释。
这是一种驾驶行为。默认情况下,连接到副本集的客户端将尝试连接到主节点。在这种情况下,连接到mongodb://localhost:27023/myproject?replicaSet=rs0
将首先连接到localhost:27023
并使用命令{isMaster: 1}
检查连接的节点是否为主/主节点。从结果中,它检测到localhost:27023
不是主/主,但它也有其他节点信息可用。接下来的步骤是连接到其他节点,直到找到主节点。
你应该关闭localhost:27023
,它甚至不会连接。