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 WSL2mongosh mongodb://[IP_I_found]:27017
but server selection timed out. - found WindowsHostIP from cmd ->
ipconfig
-> get IPv4 address 192.168.2.13 -> typemongosh 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.