You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

启动Spring Boot应用时遇java.sql.SQLNonTransientConnectionException求助

解决java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed错误

以下是经过验证的几种解决方案,按优先级尝试:

1. 在JDBC连接URL中添加allowPublicKeyRetrieval=true参数

这是最直接的临时解决方案,适用于开发环境。在Spring Boot的配置文件中修改数据源URL:

application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
spring.datasource.username=your_username
spring.datasource.password=your_password

application.yml:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/your_database_name?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
    username: your_username
    password: your_password

注意:allowPublicKeyRetrieval=true会带来一定安全风险,生产环境不建议使用,优先采用下面的方案。

2. 修改MySQL用户的认证插件为mysql_native_password

MySQL 8.0及以上版本默认使用caching_sha2_password认证插件,部分JDBC驱动兼容性不佳会触发该错误。通过以下步骤修改用户认证方式:

  1. 登录MySQL命令行:
mysql -u root -p
  1. 切换到系统数据库:
USE mysql;
  1. 修改目标用户的认证插件(替换your_user、your_host、your_password为实际信息):
ALTER USER 'your_user'@'your_host' IDENTIFIED WITH mysql_native_password BY 'your_password';
  1. 刷新权限生效:
FLUSH PRIVILEGES;

3. 确保MySQL驱动版本与服务器版本兼容

如果使用MySQL 8.0+,必须搭配8.0及以上版本的mysql-connector-java驱动。在Maven的pom.xml中更新依赖:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.33</version> <!-- 版本号需匹配你的MySQL服务器版本 -->
</dependency>

额外排查点

  • 确认MySQL服务正常运行,端口(默认3306)未被防火墙拦截
  • 检查数据库用户名、密码是否正确,且该用户拥有目标数据库的访问权限
  • 如果是远程数据库,确认MySQL已开启远程连接权限

内容的提问来源于stack exchange,提问作者Sayyed Wasim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 19:06:10