惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
B
Blog
Stack Overflow Blog
Stack Overflow Blog
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
Recent Announcements
Recent Announcements
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
D
DataBreaches.Net
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog
M
MIT News - Artificial intelligence
P
Proofpoint News Feed
罗磊的独立博客
L
LangChain Blog
V
Visual Studio Blog
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
宝玉的分享
宝玉的分享

Shiroha白羽的博客

Golang 踩坑 —— interface 为参数的时候传 nil 指针 Codeforces Round 925 (Div. 3) Codeforces Round 924 (Div. 2) Codeforces Round 923 (Div. 3) Codeforces Round 922 (Div. 2) Codeforces Round 921 (Div. 2) Educational Codeforces Round 161 (Rated for Div. 2) Codeforces Round 920 (Div. 3) Codeforces Round 919 (Div. 2) Hello 2024 Good Bye 2023 Codeforces Round 918 (Div. 4) 个人备份的常用 macOS 清理命令 Codeforces Round 917 (Div. 2) Pinely Round 3 (Div. 1 + Div. 2) Educational Codeforces Round 160 (Rated for Div. 2) Codeforces Round 915 (Div. 2) Codeforces Round 914 (Div. 2) Codeforces Round 913 (Div. 3) Educational Codeforces Round 159 (Rated for Div. 2) Codeforces Round 912 (Div. 2) Codeforces Round 911 (Div. 2) CodeTON Round 7 (Div. 1 + Div. 2, Rated, Prizes!) Educational Codeforces Round 158 (Rated for Div. 2) Codeforces Round 910 (Div. 2) Codeforces Round 909 (Div. 3) Codeforces Round 908 (Div. 2) Educational Codeforces Round 157 (Rated for Div. 2) C++自定义的字面量 Codeforces Round 907 (Div. 2)
Windows 通过网络访问 WSL2
Shiroha · 2021-06-07 · via Shiroha白羽的博客

localhost

当使用 localhost 时,Windows 直接访问到 WSL 内的进程,即看起来似乎是一台电脑,

127.0.0.1

当使用本地 IP 时,即使用 127.0.0.1 时,Windows 将会无法访问到 WSL,Windows 认为这是强调它自己。

JVM(SpringBoot)

由于此问题是在使用 SpringBoot 时遇到的问题,并不确定是不是 JVM 的问题还是 SpringBoot 的问题

问题

当使用 WSL2 中的 docker 来启动一个 mongo 镜像,使用的命令是

1
2
3
docker run -itd --name mongo -p 27017:27017 mongo --auth
docker exec -it mongo mongo admin
db.createUser({ user:'root',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]});

然后在 Navicat 中,可以直接使用 localhost 对此 mongo 进行连接,但是在 SpringBoot 中,无法连接到此 mongo 数据库,配置如下

SpringBoot-Before

报错信息:

1
com.mongodb.MongoSocketOpenException: Exception opening socket

后测试发现,Navicat 也无法使用 127.0.0.1 来访问 WSL,由此推测,JVM 或者 SpringBoot 是否是将 localhost 直接解析为 127.0.0.1

解决策略

直接使用 WSL 的 IP 来代替 localhost

在 WSL 中使用 ifconfig 来获取 VM 的 IP,例如下图中,应该选择 eth0 的 IP 172.31.18.91 来代替 localhost

WSL-ifconfig

测试

在 Navicat 和 SpringBoot 中,均连接数据库成功