Buy @ Amazon

Search This Blog

August 17, 2020

Connect to MongoDB on Windows host from WSL2

 It took me a day to lose my hopes on this issue to come to a conclusion that this is not possible. But then, wanted to give that last few tries, again... And lo behold, it worked!

So what should you be doing to get this working on your windows-wsl2 environment? This is what I did as workable set-up:

  1. Uninstalled MongoDB, one that was installed by windows installer. This one has its own quirks and issues with permission, leading to unexpected error of it being unable to find data directory. In my case, for some reason the installed version didn't honor the config file and was throwing up all of a sudden.
  2. Downloaded a zipped version and unzipped to to my target directory in windows. This one wouldn't come with a mongod.cnf file. So make sure, you create the default config file mongod.cnf, alongside mongod.exe and let its contents look like the one below (where you replace storage.dbPath and systemLog.path property values):
    ----
  3. Get your host windows IP address from powershell by running the command ipconfig to see an output like below:

  4. So now your mongodb connection string in your WSL2 would look like: 'mongodb://192.168.1.6:27017/myprojectdb'.
You should now be good to go!

No comments:

Post a Comment

Like it or hate it, feel free to share your feedback. Cheers!