WSL 2 cannot conect to MongoDB

Viewed 9650

I am developing a web app in my local using WSL 1 a while back. It works perfectly with MongoDB on local but after upgrading to WSL 2, I could not connect at all. I can still run the react app.

Here are some details: Version: Microsoft Windows [Version 10.0.19041.172] WSL 2: Ubuntu

How am I gonna call the MongoDB from the windows side from the WSL 2?

3 Answers

Mongodb listening on 127.0.0.1 by default.
You may want to edit your /etc/mongodb.conf to make mongodb listen on 0.0.0.0 so we can access mongodb from windows.

Or you can use this https://github.com/shayne/go-wsl2-host service to resolve your wsl 2 ip address to host name like ubuntu.wsl, then use that hostname instead of localhost

Try this

mongod.exe --bind_ip=0.0.0.0

OR with WSL IP

mongod.exe --bind_ip=192.***.**.1

Source

If you are getting authentication errors.

Go to task manager and stop all mongodb process & restart. I didn't have to change any ip or port.

Related