【MongoDB】WSL2访问宿主机的MongoDB

在Windows 11上安装MongoDB Server,WSL2访问该数据库,如何设置呢?

今天在win11上安装了MongoDB,但是在WSL2中始终无法连接

首先修改MongoDB的监听地址

b 复制代码
net:
  port: 27017
  bindIp: 0.0.0.0

管理员CMD重启MongoDB

b 复制代码
>>> net stop MongoDB
>>> net start MongoDB

发现WSL中还是无法访问

bash 复制代码
>>> nc -zv 192.168.128.1 27017

我尝试关闭防火墙

b 复制代码
>>> netsh advfirewall set allprofiles state off

发现可以访问了

bash 复制代码
>>> nc -zv 192.168.128.1 27017
Connection to 192.168.128.1 27017 port [tcp/*] succeeded!

但是防火墙完全打开有风险,所以可以只开27017端口,而且只给WSL2的网段192.168.128.0/20打开

b 复制代码
netsh advfirewall firewall add rule `
	name="MongoDB 27017 from WSL" `
	dir=in `
	action=allow `
	protocol=TCP `
	localport=27017 `
	remoteip=192.168.128.0/20

192.168.128.0这个可以通过在WSL中确定

bash 复制代码
>>> cat /etc/resolv.conf
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 192.168.128.1
相关推荐
Elastic 中国社区官方博客1 天前
跳过 MLOps:通过 Cloud Connect 使用 EIS 为自管理 Elasticsearch 提供托管云推理
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
dishugj1 天前
【oracle】RAC开启归档步骤
数据库
IndulgeCui1 天前
KingbaseES 数据库与用户默认表空间深度解析与实战分享
数据库
萧曵 丶1 天前
MongoDB 核心原理 + 高频面试题
数据库·mongodb
darling3311 天前
mysql 自动备份以及远程传输脚本,异地备份
android·数据库·mysql·adb
Bruk.Liu1 天前
(LangChain实战12):LangChain中的新型Chain之create_sql_query_chain
数据库·人工智能·sql·langchain
世界尽头与你1 天前
MySQL InnoDB的 MVCC 实现机制
数据库·mysql
青树寒鸦1 天前
wsl的docker备份mongo和迁移
运维·mongodb·docker·容器
你刷碗1 天前
基于S32K144 CESc生成随机数
android·java·数据库
松涛和鸣1 天前
70、IMX6ULL LED驱动实战
linux·数据库·驱动开发·postgresql·sqlserver