0

I want to be able to connect locally installed MongoDB Compass application (installed on Windows 10) to the MongoDB server which is installed in WSL2 (and no local, Windows installation for MongoDB). However, they don't connect possibly due to differences between what the OS and WSL2 views as localhost

  • Linux Distro: Ubuntu 18.04
  • Using MongoDB: 6.0.5
  • Using Mongosh: 1.8.2

Currently able to:

  • use MongoDB service commands such as sudo service mongodb start in WSL2
  • enter mongosh and create a database called "myNewDB" with new collection in it "myNewCollection"

Here's the connection string that is outputted when I enter mongosh in WSL2: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2

When opening MongoDB Compass, the URI is defaulted to mongodb://localhost:27017 . When connected, it does not show the same database(s) as from WSL2, only the 3 default admin, config, and *local *databases which I'm sure is different anyway (no appearance of the "myNewDB" database created in WSL2). So it's clear these are not the same servers, despite both of them appearing as localhost

What I've tried:

  • I followed the instructions found in #69494737 and #63452108 to restart WSL2 using wsl --shutdown and disabling fast-startup on Windows but it didn't resolve the issue.
  • I did cat /etc/resolv.conf to get the IP address of my host machine (it was different than the usual 127.0.0.1) and then connect to it through WSL2 mongosh mongodb://[IP_I_found]:27017 but server selection timed out.
  • found WindowsHostIP from cmd -> ipconfig -> get IPv4 address 192.168.2.13 -> type mongosh mongodb://192.168.2.13:27017 on WSL2 but timed out.

Edit: Sort of found a work-around for my particular use-case. At the end of the day, I just need some sort of GUI to see the database. I use VScode for editing. It has a MongoDB extension that I added onto a WSL2 instance of VSCode (not local VSCode), which connects to my MongoDB server installed in WSL2. With the extention, I'm able to visually see the database directly on the IDE. That being said, the original problem of connecting to Windows-local Compass with WSL2 Compass is still prevalent.

  • File `/etc/resolv.conf` does not provide the IP address of your machine. Use `ifconfig` (or `ipconfig` on Windows). A computer has several IP addresses and IP `127.0.0.1` always points to loopback IP, i.e. the local machine. `localhost` is an alias for loopback IP, it may resolve to local IPv4 or IPv6 address. – Wernfried Domscheit May 11 '23 at 06:38
  • This may have been a permissions thing. Try stopping the mongod service in WSL and then try connecting with compass again, using the default URL. If it fails to connect when the service is not running in WSL, but does connect when it is, that is some pretty good circumstantial evidence that it is the same service. – Joe May 12 '23 at 00:32

0 Answers0