最近为一个spring-boot项目下了mysql-9.3.0,结果因为mysql版本太新一直报错连不上。
错误如下:
2025-06-01 16:19:43.516 ERROR 22088 --- [http-nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.] with root cause
com.mysql.cj.core.exceptions.WrongArgumentException: Unable to load authentication plugin 'caching_sha2_password'.
这是因为认证插件不同造成的
8版本前是:default_authentication_plugin = mysql_native_password
8版本后是:default_authentication_plugin = caching_sha2_password
解决办法:更新驱动到最新
(或者也可以直接换一个低版本的mysql。下面是更新驱动的做法。)
步骤一:去官方下载一个匹配版本的connector。(因为我的maven仓库里没有这么高版本的,所以自己下载)
官方下载链接:MySQL :: Download Connector/J
步骤二:解压下载的zip文件,把里面的mysql-connector-j-9.30.jar放到你的项目路径下。(比如说 项目根路径/lib下)
步骤三:在pom.xml中引用你下载的mysql连接器。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>9.3.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/mysql-connector-j-9.3.0.jar</systemPath>
</dependency>
就可以正常连接到mysql了。