WSL2 的 MongoDB 指南针问题 [英] MongoDB Compass issue with WSL2
问题描述
我在 WSL2 的容器中运行了一个 MongoDB 实例.
I got a MongoDB instance running inside a container inside WSL2.
当我尝试在浏览器中的 localhost:27017
中执行请求时,即使它显示错误也能正常工作,因为我尝试使用 HTTP 协议访问数据库.
When I try to do a request in my browser at localhost:27017
it works even if it's display an error because I try to access to the db with a HTTP protocol.
但是,当我尝试从 Compass 使用主机名 localhost 和端口 27017 访问我的数据库时,我在 30000 毫秒后超时并且无法访问它.
But when I'm trying to access to my db from Compass with hostname localhost and port 27017, I got a timeout after 30000ms and I can't access to it.
有人知道为什么吗?
推荐答案
我只能使用 WSL2 IP 地址连接 mongoDB 实例.
I was able to connect mongoDB instance only using WSL2 IP address.
你可以通过在 WSL 中运行这个来获得它:
You can get it by running this one inside WSL:
ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | cut -d/ -f1
或者在 Windows 主机上的 powershell 中的这个:
or this one in powershell on windows host:
[Regex]::Match((wsl -- ip addr show eth0), 'inet (?<IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/').Groups["IP"].Value
坏消息:每次 WSL2 重新启动时,此 IP 都会更改.有一个 go-wsl2-host 为这个 IP 添加一个主机并保留它更新.但我没用过.
Bad news: this IP changes on every WSL2 restart. There is a go-wsl2-host that adds a host for this IP and keeps it updated. But I never used it.
这篇关于WSL2 的 MongoDB 指南针问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!