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.confreturnsnameserver 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

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
- solved an issue connect to localhost mongodb https://github.com/microsoft/WSL/issues/5486 — committed to martincorona007/Healthy-Recipes by martincorona007 4 years ago
Yet another solution, more in one place:
mongod.cfgand setbindIpto0.0.0.0(see previous post by @MKrupauskas, thank you, BTW)%Program Files%\MongoDB\Server\X.Y\bin. Then click “Network types” and select both types. Then click “Add”.cat /etc/resolv.confworks, after “nameserver” part. Or google for “wsl find host ip” there are plenty of solutions there.mongo %INSERT_HOST_IP_HERE%I had to change the binding ip in
C:\Program Files\MongoDB\Server\4.2\bin\mongod.cfgto 0.0.0.0Then I got the IP for my PC by running the command
ipconfigincmdand 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.0Worked for me 😃In WSL1:
mongodb://user:pass@localhost:27017/dbnameIn WSL2:
mongodb://user:pass@IP_HOST:27017/dbnameEdit: On the Windows side run mongod server with the argument
--ip_bind_allor--ip_bind 0.0.0.0IP_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.
Thanks this works great! but why is the address on
resolv.confdifferent from the address thats shown inip addr? for me just the nameserver fromresolve.confworksOk, I have been testing with mongodb portable and you need indeed to run the mongod server on windows with the options
--ip_bind_allor--ip_bind 0.0.0.0. After thatmongo --host IP_HOSTconnects to the server from WSL2 without issue.found a working solution, you basically replace the localhost with your windows ip address
localhost:27017will beYourWindowsIpAddress:27017referenceI 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…
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> 27017You can get the host ip (that wsl/Ubuntu uses for servers running on windows) by using thecat /etc/resolv.confthat 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.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:
bindIp: 0.0.0.0(then restart MongoDB service withservices.msc)27017ipconfig/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.
Thanks, I followed every step of this thread exactly but still got the issue when running step 8:
What you mean by this. Should I run it in terminal?
mongo %INSERT_HOST_IP_HERE%But it returnmongo: 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:27017Even after going through the entire thread and completing every process that was mentioned above, i’m still hanging on with this error.
I was hoping if someone could help and make me understand what’s going wrong.