- Start PowerShell/CMD as admin and run the following command:
mongod --dbpath="C:\Program Files\MongoDB\Server\4.2\data". --bind_ip=0.0.0.0
Your server now must be running
- From the unix terminal (Ubuntu in my case) run the following command:
cat /etc/resolv.conf
The output should be something like this (if you have automatic generation enabled, which it is by default:
# 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 122.1.1.1
And copy the direction from nameserver.
- Set the Mongo URI for your client with the IP that you have copied. E.g for Flask it would be something like this:
app.config['MONGO_URI'] = 'mongodb://122.1.1.1:27017/<your_db_name>'
The default port for a mongodb server on your local is 27017
- Run your app and the connection should work.
WSL2 docs about networking between Linux and Windows
Issue about connecting from Linux to a MongoDB server started from Windows
Mongo /data directory not found on Windows
As an additonal note, the first command may vary from your installed version of MongoDB. In my case is 4.2, but in the link above is 4.0.
Hope that in the future there is some
sudo
command for CMD so this can be run in a single Windows Terminal window instead of WT + CMD separated windows.