5

I got a MongoDB instance running inside a container inside WSL2.

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.

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.

Someone got an idea why, please ?

Spacemind
  • 156
  • 1
  • 6
  • when you say a container inside WSL2 i assume you mean a docker container inside docker desktop? and when you say do a request i assume you get the error `It looks like you are trying to access MongoDB over HTTP on the native driver port.` – Damo Feb 10 '20 at 18:10
  • Please amend you question to include what your development environment is and the versions of the software you are using, what version of windows, docker desktop, mongo, compass. what the command was to start the container, what connection strings you have tried from within compass. thanks. – Damo Feb 10 '20 at 18:11
  • any solution here ? – Amir Choubani Oct 27 '20 at 11:29

1 Answers1

3

I was able to connect mongoDB instance only using WSL2 IP address.

You can get it by running this one inside WSL:

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

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

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.

  • How did you structure your connection string, using the WSL2 IP address from the terminal command you ran? When I access mongo shell in wsl2, the string is mongodb://127.0.0.1:27017 aka localhost, which is different from the output of your command. – Dent7777 Apr 03 '21 at 16:03
  • 1
    @Dent7777 I connect from 3t studio to mongodb://172.17.133.40:27017/. your WSL2 is a VM. and mongodb listens on port 27017 there. that's why mongo shell from inside WSL2 can connect to localhost. In general WSL2 should map ports from VM to win host. But in my case it did not happen. That's why I have to use direct VM IP to connect – Denis Zavershinskiy Apr 06 '21 at 04:57
  • i have tried this solution. works from me! when I enter this command "ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | cut -d/ -f1" it returns me Internal IP of 172.*.*.* and I try this IP in compass, it Connects! but problem is that whenever my system reboots i have to run this command again, get IP and write that IP again in MongoDB compass. is there any permanent solution? – Syed Anas Shah Sep 06 '21 at 07:04
  • @SyedAnasShah you can try https://github.com/shayne/go-wsl2-host and then you'll be able connect not by IP but by host name ubuntu1804.wsl – Denis Zavershinskiy Sep 07 '21 at 05:38