WSL: Cannot connect to MongoDB server installed on Windows from WSL2

Environment

Windows build number:  Win32NT 10.0.19041.0 Microsoft Windows NT 10.0.19041.0
Your Distribution version: Release: 18.04
Whether the issue is on WSL 2 and/or WSL 1:  Linux version 4.19.104-microsoft-standard (oe-user@oe-host) (gcc version 8.2.0 (GCC)) #1 SMP Wed Feb 19 06:37:35 UTC 2020

Steps to reproduce

I cannot connect to MongoDB server running on Windows 10 from WSL2.

  • it was working in WSL1 via mongodb://user:pass@localhost:27017/dbname
  • I have tried steps mentioned in official guide
  • I have also tried Network reset

On ubuntu WSL2

  • cat /etc/resolv.conf returns nameserver 192.168.176.1
  • ping 192.168.176.1 works
  • ping www.google.com works

But i am not able to connect to mongodb on windows via same ip i.e 192.168.176.1.

mongoose
  .connect(`mongodb://user:pass@192.168.176.1:27017/dbname`,
    {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    }
  )
  .then((msg) => {
    console.log("Successfully Connected via mongoose!");
  })
  .catch((err) => console.log("Error", err));
  • Also tried bind mongodb server application to 0.0.0.0 doesn’t work wsl

Expected behavior

I should be able to connect to MongoDB server running on Windows 10 via the WSL ip returned while running cat /etc/resolv.conf

Actual behavior

Unable to connect connection timed out Error

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 15
  • Comments: 31

Commits related to this issue

Most upvoted comments

Yet another solution, more in one place:

  1. While installing MongoDB on Windows, make sure you enable Windows service, otherwise you’ll have to run it manually.
  2. After installation edit mongod.cfg and set bindIp to 0.0.0.0 (see previous post by @MKrupauskas, thank you, BTW)
  3. Go to Firewall and Network protection in Windows settings (Start -> type “Firewall” -> Enter)
  4. Click “Allow an app through firewall” link in bottom part of the window
  5. Click “Change settings”, then button “Allow another app” will be enabled. Click it. Browse for “mongod.exe” executable (in %Program Files%\MongoDB\Server\X.Y\bin. Then click “Network types” and select both types. Then click “Add”.
  6. It will add a set of rules to Defender Firewall that didn’t work for some reason when added manually.
  7. In WSL find host IP. On my machine cat /etc/resolv.conf works, after “nameserver” part. Or google for “wsl find host ip” there are plenty of solutions there.
  8. mongo %INSERT_HOST_IP_HERE%

I had to change the binding ip in C:\Program Files\MongoDB\Server\4.2\bin\mongod.cfg to 0.0.0.0

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

Then I got the IP for my PC by running the command ipconfig in cmd and copied the IPv4 Address.

I could then finally connect to my Windows MongoDB server from Linux wsl2

Thanks @onomatopellan , Starting mongoDB server as: mongod.exe --bind_ip 0.0.0.0 Worked for me 😃

In WSL1:

  • mongodb server running in Windows in port 27017
  • from WSL1 use mongodb://user:pass@localhost:27017/dbname

In WSL2:

  • mongodb server running in Windows in port 27017
  • from WSL2 use mongodb://user:pass@IP_HOST:27017/dbname

Edit: On the Windows side run mongod server with the argument --ip_bind_all or --ip_bind 0.0.0.0 IP_HOST is the IP of your PC you can get with ipconfig.exe. If the Firewall asks for permission say yes to private and public.

In addition to the above solution of bind_ip 0.0.0.0, I was able to connect by allowing my path to mongod.exe to the inbound rules on (Windows firewall Firewall -> Advanced Settings). I don’t know how safe it is but it is working for now.

image

Yet another solution, more in one place:

  1. While installing MongoDB on Windows, make sure you enable Windows service, otherwise you’ll have to run it manually.
  2. After installation edit mongod.cfg and set bindIp to 0.0.0.0 (see previous post by @MKrupauskas, thank you, BTW)
  3. Go to Firewall and Network protection in Windows settings (Start -> type “Firewall” -> Enter)
  4. Click “Allow an app through firewall” link in bottom part of the window
  5. Click “Change settings”, then button “Allow another app” will be enabled. Click it. Browse for “mongod.exe” executable (in %Program Files%\MongoDB\Server\X.Y\bin. Then click “Network types” and select both types. Then click “Add”.
  6. It will add a set of rules to Defender Firewall that didn’t work for some reason when added manually.
  7. In WSL find host IP. On my machine cat /etc/resolv.conf works, after “nameserver” part. Or google for “wsl find host ip” there are plenty of solutions there.
  8. mongo %INSERT_HOST_IP_HERE%

Thanks this works great! but why is the address on resolv.conf different from the address thats shown in ip addr? for me just the nameserver from resolve.conf works

Ok, I have been testing with mongodb portable and you need indeed to run the mongod server on windows with the options --ip_bind_all or --ip_bind 0.0.0.0. After that mongo --host IP_HOST connects to the server from WSL2 without issue.

found a working solution, you basically replace the localhost with your windows ip address localhost:27017 will be YourWindowsIpAddress:27017 reference

I tried SOOOO many things and nothing seemed to work, til I finally tried the steps outlined by by sylvix and sarunmrzn!!!

Something about using the Windows settings firewall (instead of just using Windows Defender Firewall) worked!!!

The instructions for that part was a little confusing so let me clarify it a little.

To get to the RIGHT firewall config…

  • Right Click the Windows Start icon (at the bottom left)
  • Click on “Settings”
  • In the search box, type then click “Firewall & network protection”
  • Then click on the “Allow an app through firewall”
  • From there you can follow the steps outlined by sylvix and sarunmrzn

Don’t forget the bind to 0.0.0.0 setting in mongod.cfg file. Probably should turn off the mongodb service (if running before doing any of this). Then turn the MongoDB service and test. BTW, a quick and easy test would be to use telnet on the wsl/Ubuntu command line telnet <host ip from> 27017 You can get the host ip (that wsl/Ubuntu uses for servers running on windows) by using the cat /etc/resolv.conf that is mentioned in this thread. If the telnet command just sits there waiting, then not good. If you see a connection, then you are good.

What worked for me was this:

In WSL, from the terminal, find the outgoing Windows IP address. ip route show | grep -i default | awk '{ print $3}' In my case, it’s 172.22.0.1 Then, since I have MongoDB 5 installed, which I installed with an .msi file, go to C:\Program Files\MongoDB\Server\5.0\bin Edit the mongod.cfg file.

net:
 port: 27017
 bindIp: 127.0.0.1,172.22.0.1 

Set the IP to 172.22.0.1 because if I set it to 0.0.0.0, MongoDB opens to all networks. This means that if you ever connect to a public Wi-Fi network and someone scans the ports, and you have the default configuration, they will be able to connect to your MongoDB and view your documents.

Then, in the Windows Firewall, Control Panel\All Control Panel Items\Windows Defender Firewall, in Advanced Settings, create a new rule of entry, which is called MongoDB, in the Protocol and Port tab, set the TCP port to 27017 and select Private and Public in Advanced Options. That worked for me.

From the WSL terminal, I run Mongosh mongodb://172.22.0.1:27017/ and it connects fine.

Note: When updating the mongod.cfg file, it will ask for administrator access. After updating this file, you can restart the mongodb service or reboot your machine.

Sum up, I got to use all these together:

  • Set in mongod.cfg bindIp: 0.0.0.0 (then restart MongoDB service with services.msc)
  • Disable Windows firewall for MongoDB port 27017
  • MongoDB URL will work in WSL with only LAN IP from ipconfig / ifconfig, not loopback ‘localhost’ nor ‘127.0.01’

If you can’t get it to work go into services (start menu), find "MongoDB Server (MongoDB), stop the service, and start it again.

Yet another solution, more in one place:

  1. While installing MongoDB on Windows, make sure you enable Windows service, otherwise you’ll have to run it manually.
  2. After installation edit mongod.cfg and set bindIp to 0.0.0.0 (see previous post by @MKrupauskas, thank you, BTW)
  3. Go to Firewall and Network protection in Windows settings (Start -> type “Firewall” -> Enter)
  4. Click “Allow an app through firewall” link in bottom part of the window
  5. Click “Change settings”, then button “Allow another app” will be enabled. Click it. Browse for “mongod.exe” executable (in %Program Files%\MongoDB\Server\X.Y\bin. Then click “Network types” and select both types. Then click “Add”.
  6. It will add a set of rules to Defender Firewall that didn’t work for some reason when added manually.
  7. In WSL find host IP. On my machine cat /etc/resolv.conf works, after “nameserver” part. Or google for “wsl find host ip” there are plenty of solutions there.
  8. mongo %INSERT_HOST_IP_HERE%

Thanks, I followed every step of this thread exactly but still got the issue when running step 8:

$ cat /etc/resolv.conf 
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.22.64.1
$ mongo 172.22.64.1
MongoDB shell version v5.0.22
connecting to: mongodb://172.22.64.1:27017/test?compressors=disabled&gssapiServiceName=mongodb
Error: network error while attempting to run command 'isMaster' on host '172.22.64.1:27017'  :
connect@src/mongo/shell/mongo.js:372:17
@(connect):2:6
exception: connect failed
exiting with code 1

8. mongo %INSERT_HOST_IP_HERE%

What you mean by this. Should I run it in terminal? mongo %INSERT_HOST_IP_HERE% But it return mongo: The term 'mongo' is not recognized as a name of a cmdlet, function, script file, or executable program.

Run mongosh on wsl mongosh.exe 172.28.112.1 Current Mongosh Log ID: 63f0c5d935196bb906f09cc1 Connecting to: mongodb://172.28.112.1:27017/?directConnection=true&appName=mongosh+1.7.1 MongoNetworkError: connect ECONNREFUSED 172.28.112.1:27017

Even after going through the entire thread and completing every process that was mentioned above, i’m still hanging on with this error. image I was hoping if someone could help and make me understand what’s going wrong.