新配置1中没有描述副本集my-mongo-set映射到此节点的主机 [英] No host described in new configuration 1 for replica set my-mongo-set maps to this node
问题描述
我正在使用Docker设置MongoDB集群。
I'm using Docker to setup a MongoDB Cluster.
我有一个bash脚本,该脚本启动了三个共享一个网桥的容器,以便它们可以与每个容器进行通信其他。我可以连接到网络中的所有MongoDB数据库。
I've a bash script which starts three containers which share a network bridge so they can talk to each other. I can connect to all the MongoDB databases in the network.
NodeJS脚本将群集的成员添加到网络中。
The NodeJS script adds the members of the cluster to the network.
docker rm -v -f mongo0
docker rm -v -f mongo1
docker rm -v -f mongo2
docker network create my-mongo-cluster
docker run -d \
-p 30002:27017 \
--name mongo0 \
--net my-mongo-cluster \
mongo mongod --replSet my-mongo-set
docker run -d \
-p 30003:27017 \
--name mongo1 \
--net my-mongo-cluster \
mongo mongod --replSet my-mongo-set
docker run -d \
-p 30004:27017 \
--name mongo2 \
--net my-mongo-cluster \
mongo mongod --replSet my-mongo-set
node setupDatabase.js
NodeJS脚本:
async function configure() {
let database = await connect();
let adminDb = database.admin();
await addMembers(adminDb);
await returnStatus(adminDb);
}
async function connect() {
return await MongoClient.connect("mongodb://localhost:30002/test");
}
async function addMembers(admin) {
var config = {
"_id" : "my-mongo-set",
"members" : [
{
"_id" : 0,
"host" : "mongo0:30002"
},
{
"_id" : 1,
"host" : "mongo1:30003"
},
{
"_id" : 2,
"host" : "mongo2:30004"
}
]};
return admin.command({replSetInitiate : config});
}
async function returnStatus(admin) {
return admin.command({replSetGetStatus : 1});
}
每当我调用 addMembers
我收到以下错误:
Whenever I invoke addMembers
I get the following error:
在新配置1中没有描述副本集my-mongo-set映射到该节点的主机
No host described in new configuration 1 for replica set my-mongo-set maps to this node
根据我的理解,这意味着MongoDB无法连接到给定成员。但是他们都有自己的名字并共享一个网桥。因此它们应该能够相互连接。
From my understanding this means that MongoDB cannot connect to the given member. But they all have their own name and share a network bridge. So they should be able to connect to each other.
任何想法导致此问题的原因?
Any ideas what is causing this issue?
推荐答案
复制设置配置是容器到容器的,因此需要内部地址和端口 container:27017
The replication set configuration is container to container so requires the internal address and port container:27017
var config = {
"_id" : "my-mongo-set",
"members" : [
{
"_id" : 0,
"host" : "mongo0:27017"
},
{
"_id" : 1,
"host" : "mongo1:27017"
},
{
"_id" : 2,
"host" : "mongo2:27017"
}
]};
可以通过Docker映射的端口进行连接,但是随后您需要使用一个主机名/地址Docker主机可以响应。它还将包括不必要的网络跃点,以跳出并返回到容器中。
It's possible to connect via the Docker mapped ports but then you would need to use a hostname/address that the Docker host can respond to. It would also include unnecessary network hops to get out and back into the containers.
这篇关于新配置1中没有描述副本集my-mongo-set映射到此节点的主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!