Skip to main content

Connecting to MongoDB from WSL2

My application runs a web server connected to a MongoDB database. For all my development needs, I am using WSL on windows 10.

Unfortunately, MongoDB is not available on WSL, so I have installed the Windows version and pointed my server to the localhost:27017 address, no issue.

It worked well until the day I switched to WSL2. Then I would get some ECONNREFUSED errors. To solve this problem, I have found a simple three steps solution.

 The whole idea is that WSL2 now uses a true VM and got its own network interface (IP, MAC address, etc.). I think it's a bit more complicated than that, but that's how I understand it. Long story short, you now have a windows IP, and a WSL IP.

1. Accessing  the windows host from WSL

The first step is to make sure that you can ping the windows IP from WSL. It was not working for me right away. It seems like there are still inconsistencies in the WSL implementation, but this github issue thread gave a good solution. It's a script yo have to run in power shell:

echo "Restarting WSL Service"
Restart-Service LxssManager
echo "Restarting Host Network Service"
Stop-Service -name "hns"
Start-Service -name "hns"
echo "Restarting Hyper-V adapters"
Get-NetAdapter -IncludeHidden | Where-Object `
    {$_.InterfaceDescription.StartsWith('Hyper-V Virtual Switch Extension Adapter')} `
    | Disable-NetAdapter -Confirm:$False
Get-NetAdapter -IncludeHidden | Where-Object `
    {$_.InterfaceDescription.StartsWith('Hyper-V Virtual Switch Extension Adapter')} `
    | Enable-NetAdapter -Confirm:$False 

After executing this script, you should be able to ping your windows IP address from WSL. Try it out!

The Github issue seems to imply that this will happen when something changes in the windows network settings. Just to be safe, I have pasted the script in a .bat file, ready to be used again.

2. Allowing inbound traffic in the firewall

Now that we know WSL can connect to the windows host, it will be consider as a remote connection on the windows side. So we need to create a new Firewall rule.

  • Go to Windows Defender  Firewall -> Advanced Settings -> Inbound rule -> New rule.
  • Select Program, then Next.
  • Copy the path to mongo.exe on Windows, Next
  • Pick Allow connection, Next.
  • Uncheck Public so it's more secured, Next.

VoilĂ ! Your WSL connections should be able to go through.

 3. Telling the mongoDB server to allow remote connections

From MongoDB perspective, a remote user will try to access it, and this is forbidden by default. You have to open the mongo.cfg file and find the network interface section. Replace 127.0.0.1 by 0.0.0.0. Save the file as an administrator, and then restart the mongoDB service.

That's it! Hopefully you are now able to access Mongo DB from your server.




Comments

Popular posts from this blog

Windows 11, bootable USB device not detected

 I recently needed to install a linux server on a new computer. The current operating system was windows 11. I decided to go with Ubuntu server. After following their instructions on how to download and create an .ISO image with Rufus , I had a problem: my USB key was not detected by the new computer as a bootable key. It turned our that the solution was the USB version. My USB key was 2.0. After doing exactly the same operation on a USB 3.0 device, everything worked fine. Bonus tip: on windows, to boot from a device, it's possible to restart the computer while holding the SHIFT key. The computer will restart and then display a menu from which you can access the different ways of booting your system. This menu will include any bootable device. Hope it helps!

Installing Postgres on Linux Lite (Ubuntu)

I have followed these instructions from the Postgres documentation : # Create the file repository configuration: sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' # Import the repository signing key: wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - # Update the package lists: sudo apt-get update # Install the latest version of PostgreSQL. # If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql': sudo apt-get -y install postgresql   After that, I was having trouble authenticating to Postgresql after installing the db server on Linux Lite.  This stackoverflow answer was very helpful. Open the file pg_hba.conf . For Ubuntu, use for example /etc/postgresql/13/main$ sudo nano pg_hba.conf and change this line at the bottom of the file, it should be the first line of the settings: local all ...

Fixing x-invalid-end-tag lint error on Vuejs

  My situation is: Coding with vue in a Vue file. Using the Vetur plugin in VS code It then generates an error of type x-invalid-end-tag in a quite random fashion. To solve it, I have found one simple trick in this github issue: // .eslintrc.js module.exports = {  //... rules:  { 'vue/no-parsing-error': [2, { 'x-invalid-end-tag': false }]  }  }