问题描述:
把nacos部署到k8s,却连接不上统一namespace下的mysql,nacos日志如下:

+ exec /usr/lib/jvm/java-1.8-openjdk/bin/java -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection '-XX:CMSInitiatingOccupancyFraction=70' -XX:+CMSParallelRemarkEnabled '-XX:SoftRefLRUPolicyMSPerMB=0' -XX:+CMSClassUnloadingEnabled '-XX:SurvivorRatio=8' -Xms1g -Xmx1g -Xmn512m '-Dnacos.standalone=true' '-Dnacos.core.auth.enabled=false' '-Dnacos.member.list=' -Xloggc:/home/nacos/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation '-XX:NumberOfGCLogFiles=10' '-XX:GCLogFileSize=100M' '-Dloader.path=/home/nacos/plugins,/home/nacos/plugins/health,/home/nacos/plugins/cmdb,/home/nacos/plugins/selector' '-Dnacos.home=/home/nacos' -jar /home/nacos/target/nacos-server.jar '--spring.config.additional-location=file:/home/nacos/conf/' '--spring.config.name=application' '--logging.config=/home/nacos/conf/nacos-logback.xml' '--server.max-http-header-size=524288'
OpenJDK 64-Bit Server VM warning: UseCMSCompactAtFullCollection is deprecated and will likely be removed in a future release.

          ,--.
        ,--.'|
    ,--,:  : |                                           Nacos 2.5.2
,`--.'`|  ' :                       ,---.               Running in stand alone mode, All function modules
|   :  :  | |                      '   ,'\   .--.--.    Port: 8848
:   |   \ | :  ,--.--.     ,---.  /   /   | /  /    '   Pid: 1
|   : '  '; | /       \   /     \.   ; ,. :|  :  /`./   Console: http://[脱敏IP]:8848/nacos/index.html
'   ' ;.    ;.--.  .-. | /    / ''   | |: :|  :  ;_
|   | | \   | \__\/: . ..    ' / '   | .; : \  \    `.      https://nacos.io
'   : |  ; .' ," .--.; |'   ; :__|   :    |  `----.   \
|   | '`--'  /  /  ,.  |'   | '.'|\   \  /  /  /`--'  /
'   : |     ;  :   .'   \   :    : `----'  '--'.     /
;   |.'     |  ,     .-./\   \  /            `--'---'
'---'        `--`---'     `----'

2026-02-02 21:32:40,497 INFO Tomcat initialized with port(s): 8848 (http)

2026-02-02 21:32:40,580 INFO Root WebApplicationContext: initialization completed in 2826 ms

2026-02-02 21:32:46,997 WARN Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'namespaceControllerV2' defined in URL [jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/classes!/com/alibaba/nacos/console/controller/v2/NamespaceControllerV2.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'namespaceOperationService' defined in URL [jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-core-2.5.2.jar!/com/alibaba/nacos/core/service/NamespaceOperationService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'externalOtherPersistServiceImpl' defined in URL [jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-core-2.5.2.jar!/com/alibaba/nacos/core/namespace/repository/ExternalNamespacePersistServiceImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.nacos.core.namespace.repository.ExternalNamespacePersistServiceImpl]: Constructor threw exception; nested exception is java.lang.RuntimeException: java.lang.RuntimeException: [db-load-error]load jdbc.properties error

2026-02-02 21:32:47,027 INFO Nacos Log files: /home/nacos/logs
2026-02-02 21:32:47,027 INFO Nacos Log files: /home/nacos/conf
2026-02-02 21:32:47,027 INFO Nacos Log files: /home/nacos/data

2026-02-02 21:32:47,028 ERROR Startup errors : 

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'namespaceControllerV2' defined in URL [jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/classes!/com/alibaba/nacos/console/controller/v2/NamespaceControllerV2.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'namespaceOperationService' defined in URL [jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-core-2.5.2.jar!/com/alibaba/nacos/core/service/NamespaceOperationService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'externalOtherPersistServiceImpl' defined in URL [jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-core-2.5.2.jar!/com/alibaba/nacos/core/namespace/repository/ExternalNamespacePersistServiceImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.nacos.core.namespace.repository.ExternalNamespacePersistServiceImpl]: Constructor threw exception; nested exception is java.lang.RuntimeException: java.lang.RuntimeException: [db-load-error]load jdbc.properties error
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'namespaceOperationService' defined in URL [jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-core-2.5.2.jar!/com/alibaba/nacos/core/service/NamespaceOperationService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'externalOtherPersistServiceImpl' defined in URL [jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-core-2.5.2.jar!/com/alibaba/nacos/core/namespace/repository/ExternalNamespacePersistServiceImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.nacos.core.namespace.repository.ExternalNamespacePersistServiceImpl]: Constructor threw exception; nested exception is java.lang.RuntimeException: java.lang.RuntimeException: [db-load-error]load jdbc.properties error
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'externalOtherPersistServiceImpl' defined in URL [jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-core-2.5.2.jar!/com/alibaba/nacos/core/namespace/repository/ExternalNamespacePersistServiceImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.nacos.core.namespace.repository.ExternalNamespacePersistServiceImpl]: Constructor threw exception; nested exception is java.lang.RuntimeException: java.lang.RuntimeException: [db-load-error]load jdbc.properties error
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.nacos.core.namespace.repository.ExternalNamespacePersistServiceImpl]: Constructor threw exception; nested exception is java.lang.RuntimeException: java.lang.RuntimeException: [db-load-error]load jdbc.properties error
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: [db-load-error]load jdbc.properties error
Caused by: java.lang.RuntimeException: [db-load-error]load jdbc.properties error
Caused by: java.io.IOException: java.lang.RuntimeException: java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
Caused by: java.lang.RuntimeException: java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
Caused by: java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
Caused by: com.mysql.cj.exceptions.CJException: Public Key Retrieval is not allowed
Caused by: com.mysql.cj.exceptions.UnableToConnectException: Public Key Retrieval is not allowed

2026-02-02 21:32:47,029 ERROR Nacos failed to start, please see /home/nacos/logs/nacos.log for more details.

2026-02-02 21:32:47,029 ERROR Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'namespaceControllerV2' defined in URL [jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/classes!/com/alibaba/nacos/console/controller/v2/NamespaceControllerV2.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'namespaceOperationService' defined in URL [jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-core-2.5.2.jar!/com/alibaba/nacos/core/service/NamespaceOperationService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'externalOtherPersistServiceImpl' defined in URL [jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-core-2.5.2.jar!/com/alibaba/nacos/core/namespace/repository/ExternalNamespacePersistServiceImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.nacos.core.namespace.repository.ExternalNamespacePersistServiceImpl]: Constructor threw exception; nested exception is java.lang.RuntimeException: java.lang.RuntimeException: [db-load-error]load jdbc.properties error


解决办法:
        修改nacos 对应yaml中的环境变量如下:

your-mysql和your-config记得改为你对应的名字

 -----其余不变----------------------------------   
         env:
            -----其余不变-----
            # 删掉 MYSQL_SERVICE_HOST 等分散变量,改用完整的 JDBC URL
            - name: MYSQL_SERVICE_URL
              value: "jdbc:mysql://your-mysql:3306/your-config?characterEncoding=utf8&connectTimeout=10000&socketTimeout=30000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true"
            -----其余不变-----

以下是深层原因拆解(个人见解):


1.  MySQL 8.0 的安全验证问题(最关键)

在你的 URL 中有一串:allowPublicKeyRetrieval=true。

MySQL 8.0 默认使用 caching_sha2_password 身份验证,这要求客户端要么使用 SSL,要么使用 RSA 公钥加密。

Nacos 容器在 K8s 内部连接时通常不带 SSL 证书。如果没有这个参数,MySQL 会直接拒绝连接。通过显式开启这个参数,允许驱动程序自动从服务器请求公钥,从而完成握手。

2. 时区不一致导致的连接拒绝

参数:serverTimezone=Asia/Shanghai。

MySQL 8.x 驱动对时区非常敏感。如果 Nacos 容器的时区与 MySQL 容器设置的时区不匹配(比如一个是 UTC,一个是 CST),驱动程序在校验连接时会抛出异常并断开。

手动指定后,强行统一了双方的对话语言,消除了时区冲突引发的报错。

3.脚本拼接的 Bug

Nacos 官方 Docker 镜像的脚本(start.sh)在处理 MYSQL_SERVICE_HOST 等分散变量时,会自动拼接一个默认的 JDBC URL。

默认拼接通常长这样:jdbc:mysql://your-mysql:3306/your-config?characterEncoding=utf8&connectTimeout=1000...

当设置了 MYSQL_SERVICE_URL,脚本会直接采用你提供的完整字符串,不再进行逻辑拼接,从而避开了默认配置缺失关键参数的问题。

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐