简体   繁体   English

使用 Mongo Compass 访问 WSL2 中 docker 上运行的 mongo 数据库

[英]Access mongo database running on docker in WSL2 using Mongo Compass

I'm working on a project that uses a mongo database running on docker.我正在开发一个使用在 docker 上运行的 mongo 数据库的项目。

When I run the project on windows 10, I can perfectly connect to my db via Mongo Compass using the standard connection string: mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&ssl=false .当我在 windows 10 上运行项目时,我可以使用标准连接字符串通过 Mongo Compass 完美连接到我的数据库: mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&ssl=false

However, running the exact same configuration on WSL2 makes it impossible connecting to the database via Mongo Compass.但是,在 WSL2 上运行完全相同的配置使得无法通过 Mongo Compass 连接到数据库。 However, the database runs as expected, and the app works fine - it is just the Mongo Compass <> WSL2 connections that are not working.但是,数据库按预期运行,应用程序运行良好 - 只是 Mongo Compass <> WSL2 连接不工作。 In order to connect with my WSL2 database I've tried this two connection strings:为了连接我的 WSL2 数据库,我尝试了这两个连接字符串:

  • mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&ssl=false
  • mongodb://0.0.0.1:27017/?readPreference=primary&appname=MongoDB%20Compass&ssl=false

For both cases, Mongo Compass will show DBs running on Windows 10, not on WSL2.对于这两种情况,Mongo Compass 将显示在 Windows 10 上运行的 DB,而不是在 WSL2 上运行。 Using the Windows 10 mongo shell doesn't list my WSL2 db, neither.使用 Windows 10 mongo shell 也没有列出我的 WSL2 数据库。

Also, my docker container running on WSL2 seems to be binding the correct port: 0.0.0.0:27017->27017/tcp .此外,我在 WSL2 上运行的 docker 容器似乎绑定了正确的端口: 0.0.0.0:27017->27017/tcp

Any ideas would be very appreciated.任何想法将不胜感激。

the problem is not the mongo compass, but the localhost.问题不是 mongo 指南针,而是 localhost。 even today, wsl2 localhost and windows localhost are not connected, and I couldn't find a simple way to connect.即使在今天,wsl2 localhost 和 windows localhost 也没有连接,我找不到简单的连接方法。 my only way to work with a mongo gui from wsl2(I use robo3t, but it should be the same), is to actually connect to a real online cluster with the connection string as you usually do.我使用来自wsl2的mongo gui的唯一方法(我使用robo3t,但它应该是相同的),是像往常一样使用连接字符串实际连接到一个真正的在线集群。 this solution worked for some people, although not for me. 这个解决方案适用于某些人,但不适合我。 I hope Microsoft with fix this soon enough.我希望微软尽快解决这个问题。

edit: in April 2021 update for wsl2, they added the(lame, but working somehow) support for gui applications, so you can run robo3t directly from wsl, and use the localhost.编辑:在 2021 年 4 月对 wsl2 的更新中,他们添加了对 gui 应用程序的(蹩脚,但以某种方式工作)支持,因此您可以直接从 wsl 运行 robo3t,并使用 localhost。 but still, very buggy, and turns off every 1 minute or so.但仍然非常麻烦,每 1 分钟左右关闭一次。

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

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