发布于2019年07月16日
报错信息
1、报错出现Unable to create initial connections of pool. (错误的原因是pom文件配置的mysql版本号的问题)
2、报错出现Caused by: java.sql.SQLException: Unable to load authentication plugin ‘caching_sha2_password’.
3、报错出现MySQLNonTransientConnectionException: Could not create connection to database server.
4、报错出现com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Public Key Retrieval is not allowed
解决措施
自从半年前装上了mysql 8.0的新版本,项目就没正常链接过mysql数据库,遇到这个问题很棘手,又是改配置又是改jdbc参数,最后重装了几遍还是没有弄好,今天总算踩了尽可能多采的坑,把问题解决了。
方案一:更改jdbc的相关参数和相关链接器的版本依赖。
方案二:更改mysql安装目录下my.ini
的配置信息。
方案一
【更改jdbc的相关参数和相关链接器的版本依赖】
1、找到设置链接数据库参数的.properties
文件,部分代码如下所示:
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true;autoReconnect=true;autoReconnectForPools=true;characterEncoding=utf8
2、修改db.driverClassName
属性值为com.mysql.cj.jdbc.Driver
将;
改为&
,db.url
中加入useSSL=false&serverTimezone=UTC
,如果遇到第四种报错情况可以再加上allowPublicKeyRetrieval=true
,如下所示:
db.driverClassName=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://127.0.0.1:3306/test?allowPublicKeyRetrieval=true&useUnicode=true&autoReconnect=true&autoReconnectForPools=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
3、如果采用了版本控制工具需修改依赖的版本
修改为如下的8.0以上的版本均可
<properties>
......
<mysql-connector.version>8.0.11</mysql-connector.version>
......
</properties>
<!--mysql 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${
mysql-connector.version}</version>
</dependency>
4、重新启动项目服务即可
方案二
【更改mysql安装目录下my.ini的配置信息】
网上有很多解决方式是采用更改my.ini
配置文件来解决的,由于mysql 8.0版本在安装目录下没有my.ini
配置文件,可通过WIN + R调用运行,输入%programdata%即可进入mysql目录下找到my.ini配置文件。
在文件中找到default_authentication_plugin
属性修改为mysql_native_password
。
将代码片段修改为如下所示:
# The default authentication plugin to be used when connecting to the server
default_authentication_plugin=mysql_native_password
修改完my.ini
文件后需要重新启动计算机。
【备注】我先采用方案二修改了之后也重启了计算机,但是没有修改好,于是采用了方案一,修改好了,建议读者先采用方案一。
本文如有不正确的地方,欢迎指出,欢迎交流。