Skip to content

Connect MongoDB on Windows from WSL2

Posted on:December 16, 2024 at 09:33 PM

tp.web.random_picture

Quick guide to connect MongoDB on Windows from WSL2.

Table of contents

Open Table of contents

Introduction

Recently, I encountered an issue connecting to MongoDB running on Windows from WSL2 (Windows Subsystem for Linux). Being new to the Windows and WSL ecosystem after using Apple machines for 15 years, I spent about 4 hours troubleshooting. After diving through various online threads, I managed to resolve the issue using the following steps.

Steps

Stop the MongoDB Service

Locate MongoDB configuration file

Typically it’s found at C:\Program Files\MongoDB\Server\8.0\bin. Replace 8.0 with your installed MongoDB version.

Edit mongod.cfg file

 bindIp: 127.0.0.1

to

bindIp: 0.0.0.0

Allow MongoDB through Windows Firewall

Restart the MongoDB service

Find your Windows host IPv4 address

Update MongoDB URI in WSL2

mongodb://<your-host-ipv4-address>:27017/<your-db-name>

Conclusion

By following these steps, I successfully connected to a local MongoDB instance running on Windows from my Serverless API project running inside WSL2. 🎉