mongodb - 容器中的 MongoDb ReplicaSet,可在桥接器和主机网络上访问
问题描述
我在名为 的容器中运行 3 个 MongoDB 实例mongodb-local
,并使用以下命令将它们作为副本集启动:
mongo --port 27020 --eval 'rs.initiate({_id: "rs1", members: [ { _id: 0, host: "mongodb-local:27018"}, { _id: 1, host: "mongodb-local:27019"}, { _id: 2, host: "mongodb-local:27020"} ] })'
这允许 MongoDB 实例与桥接网络上的其他服务作为mongodb://mongodb-local:27018,mongodb-local:27019,mongodb-local:27020/?replicaSet=rs1
但是,我还想从主机/公共连接到 MongoDB 副本集。我已经在那里发布了 3 个端口,但是尝试连接和使用mongodb://localhost:27018,localhost:27019,localhost:27020/?replicaSet=rs1
失败,因为副本彼此知道mongodb-local
,这对于主机是不可解析的。
例如 c# 驱动程序中的错误,例如:
One or more errors occurred. (A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/localhost:27018" }", EndPoint: "Unspecified/localhost:27018", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.
或在外壳中
...>mongo mongodb://localhost:27020,localhost:27019,localhost:27018/?replicaSet=rs1
MongoDB shell version v4.0.2
connecting to: mongodb://localhost:27020,localhost:27019,localhost:27018/?replicaSet=rs1
2020-07-22T11:24:23.036+0100 I NETWORK [js] Starting new replica set monitor for rs1/localhost:27020,localhost:27019,localhost:27018
2020-07-22T11:24:23.042+0100 I NETWORK [js] Successfully connected to localhost:27018 (1 connections now open to localhost:27018 with a 5 second timeout)
2020-07-22T11:24:23.042+0100 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to localhost:27019 (1 connections now open to localhost:27019 with a 5 second timeout)
2020-07-22T11:24:23.046+0100 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to localhost:27020 (1 connections now open to localhost:27020 with a 5 second timeout)
2020-07-22T11:24:23.048+0100 I NETWORK [ReplicaSetMonitor-TaskExecutor] changing hosts to rs1/mongodb-local:27018,mongodb-local:27019,mongodb-local:27020 from rs1/localhost:27018,localhost:27019,localhost:27020
2020-07-22T11:24:28.563+0100 W NETWORK [js] Unable to reach primary for set rs1
2020-07-22T11:24:28.563+0100 I NETWORK [js] Cannot reach any nodes for set rs1. Please check network connectivity and the status of the set. This has happened for 1 checks in a row.
2020-07-22T11:24:32.072+0100 W NETWORK [js] Unable to reach primary for set rs1
2020-07-22T11:24:32.072+0100 I NETWORK [js] Cannot reach any nodes for set rs1. Please check network connectivity and the status of the set. This has happened for 2 checks in a row.
2020-07-22T11:24:35.581+0100 W NETWORK [js] Unable to reach primary for set rs1
2020-07-22T11:24:35.581+0100 I NETWORK [js] Cannot reach any nodes for set rs1. Please check network connectivity and the status of the set. This has happened for 3 checks in a row.
2020-07-22T11:24:39.091+0100 W NETWORK [js] Unable to reach primary for set rs1
2020-07-22T11:24:39.092+0100 I NETWORK [js] Cannot reach any nodes for set rs1. Please check network connectivity and the status of the set. This has happened for 4 checks in a row.
2020-07-22T11:24:39.097+0100 E QUERY [js] Error: connect failed to replica set rs1/localhost:27020,localhost:27019,localhost:27018 :
connect@src/mongo/shell/mongo.js:257:13
@(connect):1:6
exception: connect failed
相反,我可以将副本集启动为:
mongo --port 27020 --eval 'rs.initiate({_id: "rs1", members: [ { _id: 0, host: "localhost:27018"}, { _id: 1, host: "localhost:27019"}, { _id: 2, host: "localhost:27020"} ] })'
这将允许来自主机/公共的连接,并且巧合的是,由于副本都在同一个容器上运行,它们也可以相互通信。
但是,其他服务无法与 连接mongodb://mongodb-local:27018,mongodb-local:27019,mongodb-local:27020/?replicaSet=rs1
,并出现类似错误。
如果我想将每个副本放在单独的容器上,这也会失败。
我相信这是因为客户端在连接后收到副本集记录,并期望它在连接的网络上可用?如果是这样,这不是一个很好的假设。
如何在桥接网络上启动副本集并允许来自桥接网络外部的连接?
解决方案
推荐阅读
- swift - 如何在 Swift / Codable 中解码日期?
- jquery - reactjs状态变化时如何正确更新jquery数据表内容?
- java - org.springframework.beans.factory.BeanCreationException:创建工厂bean时出错
- solidity - Metamask 自定义 RPC 未发送正确的合约信息
- android - Android - 毕加索获取图像高度
- python - 如何更改网页代码打开的目录TCP服务器python
- html - 如何使 firstChild 高度为 100% flexbox 包装项目
- firebase - Firebase 不会更新我对自定义域的更改
- mongodb - Mongodb Compass和shell中的mongodb查找聚合结果不同
- r - Taking values from data table rows and putting it in a cell as a list in R