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就能解决该问题。

猜你喜欢

转载自blog.csdn.net/qq_37770674/article/details/131672953