'MongoDB Issues when using WSL 2 (ERROR: connect ECONNREFUSED 127.0.0.1:27017)
I am very new to development, and so far I was following online course instructions, but now I decided to migrate all my work from GoormIDE to WebStorm. In GoormIDE MongoDB and NodeJS was installed automatically, so I had to install both on my Windows in order to make work.
I managed to run the application (you can check it out here: https://yelpcampuk.herokuapp.com/) locally, but as soon as I hit the "View all campgrounds" button, I'm getting this error: ERROR: connect ECONNREFUSED 127.0.0.1:27017.
It would be nice if I could get some help from someone who has experience using WSL2 and MongoDB in order to find out what the issue might be. And yes, MongoDB is running in Windows command as it cannot be run on Ubuntu 20.04 using WSL2 (but correct me if I'm wrong)
That is how I connect to the DB in GoormIDE and it works perfectly:
var url = process.env.DATABASEURL || "mongodb://127.0.0.1:27017/YelpCamp"
mongoose.set("useUnifiedTopology", true);
mongoose.connect(url, {
useNewUrlParser: true,
useCreateIndex: true
}).then(() => {
console.log('Connected to DB!');
}).catch(err => {
console.log('ERROR:', err.message);
});
But when I try to run in on my laptop via WebStorm (or VS Code), I'm only getting the error stated above.
Any suggestions are welcome because I spent many hours of research and I couldn't really get closer to the solution yet...
P.s.: I only started learning to code 2 months ago, so if I wrote something silly, please forgive me! Thanks all!
Solution 1:[1]
This worked for me:
mongod --bind_ip 0.0.0.0 on the windows CMD
I was able to connect to it through WSL2. Check if you have other MongoDB Servers running, I had to end each task before running the command.
Solution 2:[2]
After doing a lot of research I found the right way to do it on this Github thread: https://github.com/microsoft/WSL/issues/5486#issuecomment-810866629
Besides binding your ip to 0.0.0.0, the key part was that you need to connect using the host IP that WSL detects as your computer. Use cat /etc/resolv.conf on WSL terminal. (See last item 7 on the list below).
This is quoted from the above mentioned link, so all credits go to the author (sylvix). No need to reinstall mongo DB, but just for the sake of it, I'm putting all steps:
- While installing MongoDB on Windows, make sure you enable Windows service, otherwise you'll have to run it manually.
- After installation edit mongod.cfg and set bindIp to 0.0.0.0 (see previous post by @MKrupauskas, thank you, BTW)
- Go to Firewall and Network protection in Windows settings (Start -> type "Firewall" -> Enter)
- Click "Allow an app through firewall" link in bottom part of the window
- 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".
- It will add a set of rules to Defender Firewall that didn't work for some reason when added manually.
- In WSL find host IP. On my machine
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%
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gabriel Luque |
| Solution 2 | Juanu |
