最近為一個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 causecom.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了。