3

I want to work with Mongo Compass running in Windows and connect to MongoDb server in WSL2.

Both MongoDb services are working fine (I can connect from Windows to MongoDb server on Windows and from WSL2 to MongoDb server on WSL2).

Windows build 19042.

Thanks!

pilauria
  • 35
  • 2
  • 6

2 Answers2

3

Unless you are running a pretty old build (from 2018 or earlier) WSL includes a feature known as Localhost Forwarding, which should automatically allow you to use localhost in Mongo Compass in Windows to connect to the database running under WSL.

However, sometimes that features "breaks", especially if you hibernate or turn on Windows with the Fast Startup feature enabled (which is the default).

If this is the case, try wsl --shutdown and restart WSL. Then disable Fast Startup. If you do need to hibernate, remember that you may need to wsl --shutdown again to restore the forwarding mechanism.

See this answer for some additional details.

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
-1

As far, as I understood, to access something running on WSL from you host-machine(Windows) you need to manually map host:port of your WSL MongoDB server to localhost.

This link describes the topic, I hope: https://learn.microsoft.com/en-us/windows/wsl/networking#accessing-a-wsl-2-distribution-from-your-local-area-network-lan

After mapping your port you probably will connect to your MongoDB server via Compass with mapped address and port.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Note that the documentation you referenced is for accessing WSL2 from *another* machine on the network. This is not necessary for `localhost`, which is what the question asked. – NotTheDr01ds Oct 11 '21 at 04:59