gpt4 book ai didi

mongodb - 无法连接到 WSL2 内 docker 内的 mongo

转载 作者:行者123 更新时间:2023-12-03 08:11:25 67 4
gpt4 key购买 nike

我的设置有什么问题吗?

这是我的 docker-compose.yml我以 sudo docker-compose up -d 开头

最终结果是这样的:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                  NAMES
7fa1ebd185d3 mongo "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:27017-27019->27017-27019/tcp mongodb

version: "3.8"
services:
mongodb:
image: mongo
container_name: mongodb
environment:
- MONGO_INITDB_ROOT_USERNAME=root1234356asdas
- MONGO_INITDB_ROOT_PASSWORD=324gdfgdfgasdas
volumes:
- /home/user/mongodb/database:/data/db
ports:
- '27017-27019:27017-27019'
restart: unless-stopped

我尝试通过 localhost/127.0.0.1/0.0.0.0 使用 Robot 3T 连接到它,但它总是导致

“远程计算机拒绝连接”

enter image description here enter image description here enter image description here

提前致谢

编辑。

我尝试过这些:

netsh interface portproxy add v4tov4 listenport=27017 listenaddress=127.0.0.1 connectport=27017 connectaddress=172.22.245.111

netsh interface portproxy add v4tov4 listenport=27017 listenaddress=127.0.0.1 connectport=27017 connectaddress=172.17.0.1

netsh interface portproxy add v4tov4 listenport=27017 listenaddress=127.0.0.1 connectport=27017 connectaddress=172.15.0.1

netsh interface portproxy add v4tov4 listenport=27017 listenaddress=0.0.0.0 connectport=27017 connectaddress=172.22.245.111

netsh interface portproxy add v4tov4 listenport=27017 listenaddress=0.0.0.0 connectport=27017 connectaddress=172.17.0.1

netsh interface portproxy add v4tov4 listenport=27017 listenaddress=0.0.0.0 connectport=27017 connectaddress=172.15.0.1

netsh interface portproxy add v4tov4 listenport=27018 listenaddress=127.0.0.1 connectport=27018 connectaddress=172.22.245.111

netsh interface portproxy add v4tov4 listenport=27018 listenaddress=127.0.0.1 connectport=27018 connectaddress=172.17.0.1

netsh interface portproxy add v4tov4 listenport=27018 listenaddress=127.0.0.1 connectport=27018 connectaddress=172.15.0.1

netsh interface portproxy add v4tov4 listenport=27018 listenaddress=0.0.0.0 connectport=27018 connectaddress=172.22.245.111

netsh interface portproxy add v4tov4 listenport=27018 listenaddress=0.0.0.0 connectport=27018 connectaddress=172.17.0.1

netsh interface portproxy add v4tov4 listenport=27018 listenaddress=0.0.0.0 connectport=27018 connectaddress=172.15.0.1



netsh interface portproxy add v4tov4 listenport=27019 listenaddress=127.0.0.1 connectport=27019 connectaddress=172.22.245.111

netsh interface portproxy add v4tov4 listenport=27019 listenaddress=127.0.0.1 connectport=27019 connectaddress=172.17.0.1

netsh interface portproxy add v4tov4 listenport=27019 listenaddress=127.0.0.1 connectport=27019 connectaddress=172.15.0.1

netsh interface portproxy add v4tov4 listenport=27019 listenaddress=0.0.0.0 connectport=27019 connectaddress=172.22.245.111

netsh interface portproxy add v4tov4 listenport=27019 listenaddress=0.0.0.0 connectport=27019 connectaddress=172.17.0.1

netsh interface portproxy add v4tov4 listenport=27019 listenaddress=0.0.0.0 connectport=27019 connectaddress=172.15.0.1

编辑这对我有用:

连接到来自 hostname -I | cut -f 1 -d ' ' 的 IP (在 WSL 内部执行)

mongo 创建为:

docker-compose.yml

version: "3.7"
services:
mongo:
container_name: container-mongodb
image: mongo:latest
restart: always
ports:
- 27017:27017

environment:
MONGO_INITDB_ROOT_USERNAME: "jane"
MONGO_INITDB_ROOT_PASSWORD: "secret"
MONGO_INITDB_DATABASE: "mydb"

volumes:
- ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro

mongo-init.js

print("Started Adding the Users.");
db = db.getSiblingDB("admin");
db.createUser({
user: "userx",
pwd: "1234",
roles: [{ role: "readWrite", db: "admin" }],
});
print("End Adding the User Roles.");

enter image description here

最佳答案

要从 Windows 主机连接到 Linux WSL2,您可以使用 localhost,或者当 WSL2 IP 失败时。
您可以通过 hostname -I | 从 WSL2 获取它。切-f 1 -d''

关于mongodb - 无法连接到 WSL2 内 docker 内的 mongo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70710592/

67 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com