Nacos无法连接wsl2上部署的MySQL
情况描述
在部署Nacos时,需要连接MySQL数据库管理数据,但是如果连接的是wsl2上部署的MySQL,使用127.0.0.1
或者localhost
,通常会出现如下错误:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'memoryMonitor' defined in URL [jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-config-2.1.1.jar!/com/alibaba/nacos/config/server/monitor/MemoryMonitor.class]:
Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'asyncNotifyService':
Unsatisfied dependency expressed through field 'dumpService'; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'externalDumpService':
Invocation of init method failed; nested exception is ErrCode:500, ErrMsg:Nacos Server did not start because dumpservice bean construction failure :
No DataSource set
原因分析
WSL 2网络默认使用动态IP地址。
如果MySQL容器在WSL 2中运行,Nacos无法连接到容器的动态IP地址。
解决方案
可以尝试使用如下命令获取WSL 2的静态IP地址来连接MySQL容器:
ip addr show eth0
root@LAPTOP-LOVS4PI6:~# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:15:5d:7a:cb:fc brd ff:ff:ff:ff:ff:ff
inet 172.19.236.173/20 brd 172.19.239.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::215:5dff:fe7a:cbfc/64 scope link
valid_lft forever preferred_lft forever
获取的WSL 2的静态IP地址为:172.19.236.173,添加到Nacos的配置文件的数据库路径中。如:
#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
spring.datasource.platform=mysql
### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:mysql://172.19.236.173:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=kun_mysql
重新启动Nacos就能解决该问题。
天河有尽身作涯,星海无边前是岸。
分类:
SpringCloud
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2022-07-11 设计模式 03 抽象工厂模式