英文:
bitnami/mongodb docker image fails to connect when replica set enabled?
问题
我想使用bitnami/mongodb
Docker镜像在本地开发中启动一个MongoDB服务器。我需要使用单节点副本集,因为我的项目使用事务。当未配置为副本集时,该镜像正常工作,但当配置为副本集时,Mongo Compass将无法连接。
使用副本集
我使用Docker Compose启动容器:
mongodb:
hostname: mongodb
image: bitnami/mongodb:latest
environment:
MONGODB_REPLICA_SET_MODE: primary
MONGODB_REPLICA_SET_NAME: rs0
MONGODB_REPLICA_SET_KEY: 12345
MONGODB_ROOT_PASSWORD: root
volumes:
- mongo-data:/data/db
restart: always
尝试使用以下URI连接会返回getaddrinfo ENOTFOUND mongodb
错误:
mongodb://localhost:27017?replicaSet=rs0
检查容器的输出,除了它收到连接尝试然后立即结束之外,似乎没有太多信息。
2023-02-16 01:16:55 {"t":{"$date":"2023-02-16T09:16:55.854+00:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:54038","uuid":"87f2d8bc-d254-4824-b1f1-c27e0e9a4558","connectionId":12,"connectionCount":6}}
2023-02-16 01:16:55 {"t":{"$date":"2023-02-16T09:16:55.855+00:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn12","msg":"client metadata","attr":{"remote":"127.0.0.1:54038","client":"conn12","doc":{"driver":{"name":"nodejs","version":"4.10.0"},"os":{"type":"Windows_NT","name":"win32","architecture":"x64","version":"10.0.22621"},"platform":"Node.js v16.5.0, LE (unified)|Node.js v16.5.0, LE (unified)","application":{"name":"MongoDB Compass Beta"}}}}
2023-02-16 01:16:55 {"t":{"$date":"2023-02-16T09:16:55.869+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn12","msg":"Connection ended","attr":{"remote":"127.0.0.1:54038","uuid":"87f2d8bc-d254-4824-b1f1-c27e0e9a4558","connectionId":12,"connectionCount":5}}
不使用副本集
如果我禁用副本集,我可以正常连接:
mongodb:
hostname: mongodb
image: bitnami/mongodb:latest
environment:
MONGODB_ROOT_PASSWORD: root
volumes:
- mongo-data:/data/db
restart: always
mongodb://root:root@localhost:27017
英文:
I want to use the bitnami/mongodb
docker image to spin up a mongodb server for local development. I need to use a single node replica set as my project uses transactions. The image works fine when not configured as a replica set, but when it is Mongo Compass wont connect.
With Replica Set
I'm using docker compose to start the container:
mongodb:
hostname: mongodb
image: bitnami/mongodb:latest
environment:
MONGODB_REPLICA_SET_MODE: primary
MONGODB_REPLICA_SET_NAME: rs0
MONGODB_REPLICA_SET_KEY: 12345
MONGODB_ROOT_PASSWORD: root
volumes:
- mongo-data:/data/db
restart: always
Trying to connect with the following URI comes back with a getaddrinfo ENOTFOUND mongodb
error:
mongodb://localhost:27017?replicaSet=rs0
Checking the container's output there doesnt seem to be much information other than it did receive the connection attempt then immediately ended.
2023-02-16 01:16:55 {"t":{"$date":"2023-02-16T09:16:55.854+00:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:54038","uuid":"87f2d8bc-d254-4824-b1f1-c27e0e9a4558","connectionId":12,"connectionCount":6}}
2023-02-16 01:16:55 {"t":{"$date":"2023-02-16T09:16:55.855+00:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn12","msg":"client metadata","attr":{"remote":"127.0.0.1:54038","client":"conn12","doc":{"driver":{"name":"nodejs","version":"4.10.0"},"os":{"type":"Windows_NT","name":"win32","architecture":"x64","version":"10.0.22621"},"platform":"Node.js v16.5.0, LE (unified)|Node.js v16.5.0, LE (unified)","application":{"name":"MongoDB Compass Beta"}}}}
2023-02-16 01:16:55 {"t":{"$date":"2023-02-16T09:16:55.869+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn12","msg":"Connection ended","attr":{"remote":"127.0.0.1:54038","uuid":"87f2d8bc-d254-4824-b1f1-c27e0e9a4558","connectionId":12,"connectionCount":5}}
Without Replica Set
If I disable the replica set I can connect just fine:
mongodb:
hostname: mongodb
image: bitnami/mongodb:latest
environment:
MONGODB_ROOT_PASSWORD: root
volumes:
- mongo-data:/data/db
restart: always
mongodb://root:root@localhost:27017
答案1
得分: 1
我通过随意尝试找到了答案。我认为发生的情况是Bitnami映像正在使用节点主机名mongodb
来初始化复制集,并在Mongo中。当连接到复制集时,似乎是将连接尝试转发到该主机名,而在Docker容器中运行时,该主机名通常具有自己的网络,并且无法在主机的网络上使用。Ping mongodb
会失败。
有两种可能的解决方法:
- 将广告主机名设置为localhost:
MONGODB_ADVERTISED_HOSTNAME: localhost
- 将其添加到您的/hosts文件中:
// C:\Windows\System32\drivers\etc\hosts
127.0.0.1 mongodb
英文:
I found my answer through just playing around. What I believe is happening is the bitnami image is initalizing the replicaset with the nodes hostname mongodb
and in mongo. When connecting to a replicaset it then seems like its forwarded to try to connect to that hostname, which when running in a docker container often has its own network and is not available on your hosts network. Pinging mongodb
fails.
There's two possible solutions to this:
- Set the advertised hostname to localhost:
MONGODB_ADVERTISED_HOSTNAME: localhost
- Add it to your /hosts file
// C:\Windows\System32\drivers\etc\hosts
127.0.0.1 mongodb
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论