简体   繁体   English

WSL2 的 MongoDB 指南针问题

[英]MongoDB Compass issue with WSL2

I got a MongoDB instance running inside a container inside WSL2.我在 WSL2 的容器内运行了一个 MongoDB 实例。

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.当我尝试在localhost:27017浏览器中执行请求时,即使它显示错误,它也能工作,因为我尝试使用 HTTP 协议访问数据库。

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.但是,当我尝试从 Compass 使用主机名 localhost 和端口 27017 访问我的数据库时,我在 30000 毫秒后超时并且无法访问它。

Someone got an idea why, please ?有人知道为什么吗?

I was able to connect mongoDB instance only using WSL2 IP address.我只能使用 WSL2 IP 地址连接 mongoDB 实例。

You can get it by running this one inside WSL:你可以通过在 WSL 中运行这个来获得它:

ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | cut -d/ -f1

or this one in powershell on windows host:或 Windows 主机上的 powershell 中的这个:

[Regex]::Match((wsl -- ip addr show eth0), 'inet (?<IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/').Groups["IP"].Value

Bad news: this IP changes on every WSL2 restart.坏消息:每次 WSL2 重新启动时,此 IP 都会更改。 There is a go-wsl2-host that adds a host for this IP and keeps it updated.有一个go-wsl2- host 为这个 IP 添加一个主机并保持更新。 But I never used it.但我从来没有用过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM