Recently, while doing some experimentation with MongoDB I noticed that my Spring app would no longer connect to mongo with the old WSL1 configuration. After some digging I was able to reconnect my app running in WSL2 to the Mongo service running on the Windows side. The following steps should get packets flowing:
-
Allow packets through the firewall by firing up a Powershell console and issuing:
New-NetFirewallRule -DisplayName "MongoDB from WSL2" -InterfaceAlias "vEthernet (WSL)" -Direction Inbound -Protocol TCP -LocalPort 27017 -Action Allow
-
Reconfigure Mongo to listen for requests on all interfaces, as this is just for local development, by editing the mongod.cfg line:
bindIp: 0.0.0.0
-
Update the spring application.properties to read something like:
spring.data.mongodb.uri=mongodb://${MONGO_HOST}:27017/
- where MONGO_HOST is a combination of the strings
echo $NAME
and .local - ex.
my_pc.local
Afterwards, your application should connect and you’re back in business.
References