简体   繁体   中英

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.

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 .

However, running the exact same configuration on WSL2 makes it impossible connecting to the database via 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. In order to connect with my WSL2 database I've tried this two connection strings:

  • 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. Using the Windows 10 mongo shell doesn't list my WSL2 db, neither.

Also, my docker container running on WSL2 seems to be binding the correct port: 0.0.0.0:27017->27017/tcp .

Any ideas would be very appreciated.

the problem is not the mongo compass, but the localhost. even today, wsl2 localhost and windows localhost are not connected, and I couldn't find a simple way to connect. 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. 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. but still, very buggy, and turns off every 1 minute or so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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