Skip to content

Instantly share code, notes, and snippets.

@eagskunst
Created August 15, 2020 17:08
Show Gist options
  • Save eagskunst/59086fd32479ede3067688a74d7e21bc to your computer and use it in GitHub Desktop.
Save eagskunst/59086fd32479ede3067688a74d7e21bc to your computer and use it in GitHub Desktop.
Run a MongoDB server from Windows and connect to it from WSL2 Ubuntu
  1. 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

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

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

  1. Run your app and the connection should work.
@eagskunst
Copy link
Author

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.

@eagskunst
Copy link
Author

If you do the steps above and it's not working, make sure MongoDB proccess is not running already.

  1. Open Task Manager
  2. Services tab, look for MongoDB
  3. Right click -> Stop

@leiberbertel
Copy link

It's been a long time coming, but it really worked for me. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment