我在this SO answer之后创建了一个本地MongoDB副本集。
docker-compose文件:
services:
mongo1:
container_name: mongo1
image: mongo:4.2
ports:
- 27017:27017
restart: always
command: ["--bind_ip_all", "--replSet", "rs" ]
mongo2:
container_name: mongo2
image: mongo:4.2
ports:
- 27018:27017
restart: always
command: ["--bind_ip_all", "--replSet", "rs" ]
mongo3:
container_name: mongo3
image: mongo:4.2
ports:
- 27019:27017
restart: always
command: ["--bind_ip_all", "--replSet", "rs" ]
replica_set:
image: mongo:4.2
container_name: replica_set
depends_on:
- mongo1
- mongo2
- mongo3
volume:
- ./initiate_replica_set.sh:/initiate_replica_set.sh
entrypoint:
- /initiate_replica_set.sh
initiate_replica_set. sh文件:
#!/bin/bash
echo "Starting replica set initialize"
until mongo --host mongo1 --eval "print(\"waited for connection\")"
do
sleep 2
done
echo "Connection finished"
echo "Creating replica set"
mongo --host mongo1 <<EOF
rs.initiate(
{
_id : 'rs0',
members: [
{ _id : 0, host : "mongo1:27017" },
{ _id : 1, host : "mongo2:27017" },
{ _id : 2, host : "mongo3:27017" }
]
}
)
EOF
echo "replica set created"
复制副本集已成功启动并正常运行,但当我尝试连接到复制副本集时,它出错:
$ mongo "mongodb://localhost:27017,localhost:27018,localhost:27019/?replicaSet=rs"
MongoDB shell version v5.0.2
connecting to: mongodb://localhost:27017,localhost:27018,localhost:27019/?compressors=disabled&gssapiServiceName=mongodb&replicaSet=rs
{"t":{"$date":"2021-08-05T21:35:40.667Z"},"s":"I", "c":"NETWORK", "id":4333208, "ctx":"ReplicaSetMonitor-TaskExecutor","msg":"RSM host selection timeout","attr":{"replicaSet":"rs","error":"FailedToSatisfyReadPreference: Could not find host matching read preference { mode: \"nearest\" } for set rs"}}
Error: Could not find host matching read preference { mode: "nearest" } for set rs, rs/localhost:27017,localhost:27018,localhost:27019 :
connect@src/mongo/shell/mongo.js:372:17
@(connect):2:6
exception: connect failed
exiting with code 1
更多详细日志:
{
"t": {
"$date": "2021-08-05T21:35:54.531Z"
},
"s": "I",
"c": "-",
"id": 4333222,
"ctx": "ReplicaSetMonitor-TaskExecutor",
"msg": "RSM received error response",
"attr": {
"host": "mongo1:27017",
"error": "HostUnreachable: Error connecting to mongo1:27017 :: caused by :: Could not find address for mongo1:27017: SocketException: Host not found (authoritative)",
"replicaSet": "rs",
"response": "{}"
}
}
问题的原因是什么,我如何解决它?
3条答案
按热度按时间wljmcqd81#
关于这个问题,各地都有一些片面的答案,以下是我认为完整的答案。
《The Cause》
虽然连接字符串是
"mongodb://localhost:27017,localhost:27018,localhost:27019/?replicaSet=rs"
,但mongo客户端不会连接到具有种子地址localhost:27017
等的副本集的成员,而是连接到从种子主机返回的副本配置集中的成员,即rs.initiate
调用中的成员。这就是为什么错误消息是Error connecting to mongo1:27017
而不是Error connecting to localhost:27017
。与mongo服务器容器在同一容器网络中的mongo客户端可以通过
mongo1:27017
地址连接到服务器;但是,位于容器网络外部的主机上的客户端无法将mongo1
解析为IP。这个问题的典型解决方案是代理,请参阅Access docker container from host using containers name了解详细信息。修复
因为这个问题涉及到Docker网络,而Docker网络在Linux和Mac之间各不相同。这两个平台上的修复是不同的。
Linux
代理修复(通过第三方软件或修改
/etc/hosts
文件)工作正常,但有时不可行,例如在远程CI主机上运行。一个简单的自包含可移植解决方案是更新intiate_replia_set.sh
脚本,以使用成员IP而不是主机名启动副本集。initiate_replia_set.sh
这样,mongo副本集成员的地址中就有容器IP而不是主机名。容器IP可以从主机访问。
或者,我们可以在docker-compose文件中显式地为每个容器分配静态IP,并在初始化副本集时使用静态IP。这是一个类似的修复,但更多的工作。
Mac
不幸的是,上述解决方案不适用于Mac,因为Mac上的Docker容器IP不会暴露在主机网络接口上。https://docs.docker.com/docker-for-mac/networking/#per-container-ip-addressing-is-not-possible
最简单的方法是在
/etc/hosts
文件中添加以下Map:l2osamch2#
docker compose
用户的其他信息。要捕获上述情况,请使用
healthcheck
,它利用ping
和URL显式定义replicaSet=
h22fl7wq3#
我认为将replicaSet的名称“rs”更改为“rs0”就足够了,这是配置中为您的replicaSet指定的名称。
您将拥有: