MySQL-8.x-问题处理汇总

● MySQL 8.0.15 报 SSLException 异常

问题描述

该问题可能会导致连接关闭,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Sun May 12 22:08:48 CST 2019 WARN: Caught while disconnecting...

EXCEPTION STACK TRACE:

** BEGIN NESTED EXCEPTION **

javax.net.ssl.SSLException
MESSAGE: closing inbound before receiving peer's close_notify

STACKTRACE:

javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify
at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:133)
at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:117)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:308)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:264)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:255)
at java.base/sun.security.ssl.SSLSocketImpl.shutdownInput(SSLSocketImpl.java:645)
at java.base/sun.security.ssl.SSLSocketImpl.shutdownInput(SSLSocketImpl.java:624)
at com.mysql.cj.protocol.a.NativeProtocol.quit(NativeProtocol.java:1319)
at com.mysql.cj.NativeSession.quit(NativeSession.java:182)
at com.mysql.cj.jdbc.ConnectionImpl.realClose(ConnectionImpl.java:1750)
at com.mysql.cj.jdbc.ConnectionImpl.close(ConnectionImpl.java:720)
at com.zaxxer.hikari.pool.PoolBase.quietlyCloseConnection(PoolBase.java:132)
at com.zaxxer.hikari.pool.HikariPool.lambda$closeConnection$1(HikariPool.java:434)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)

** END NESTED EXCEPTION **

解决方案

数据库 URL 需要声明是否使用 SSL 安全验证及指定服务器上的时区,将 useSSL 设置为 false,即:?useSSL=false&serverTimezone=UTC"

● MySQL 8.0.15 驱动类名称

问题描述

旧版驱动类是 com.mysql.jdbc.Driver,但是如果在 8.0 还继续用这个类,就会提示:

1
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

解决方案

需要用新的驱动类,在配置文件中改为:driver=com.mysql.cj.jdbc.Driver

● MySQL 8.0.15 时区指定

问题描述

驱动类正确,但是报错:

1
java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

解决方案

手动指定时区,如果服务器也是东八区(GMT+8),那么在 URL 后面加上参数 serverTimezone=GMT%2B8 即可:

1
url=jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8

● MySQL 8.0.15 getTables 默认返回所有库的表

问题描述

8.0 及以上版本的驱动默认将 nullCatalogMeansCurrent 的默认值由 true 改为了 false,如果使用 DatabaseMetaData 类的对象调用 getTables 方法,就会返回所有库的表,而非在 url 参数中指定的数据库(本例中数据库名为 test)

解决方案

手动在参数中指定 nullCatalogMeansCurrent 值为 true

1
url=jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useSSL=false&useSSL=false&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true

● SpringBoot 连接 MySql 8.x 时出现 CLIENT_PLUGIN_AUTH is required 异常

问题描述

SpringBoot 连接 MySql 8.x 时出现 CLIENT_PLUGIN_AUTH is required 异常

解决方案

修改 mysql-connector-java 依赖版本为低版本,如:

1
2
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.47'

● MySQL 8.0.15 SSL

问题描述

关闭连接对象时会产生的一个无关紧要的报错,不解决也可以正常使用,会占据控制台的大量篇幅,导致使用体验极差:

1
2
javax.net.ssl.SSLException
MESSAGE: closing inbound before receiving peer's close_notify

解决方案

MySQL 8.0 开始,数据库 URL 需要设置是否使用 SSL 安全连接,在 URL 后面加上参数 useSSL=false 即可,多个参数键值对中间用 & 隔开:

1
url=jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useSSL=false

● MySQL 8.0.15 Public Key Retrieval 报错

问题描述

重启服务器后出现 Public Key Retrieval 报错:

1
java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed

解决方案

在 URL 后加上 allowPublicKeyRetrieval=true 即可

1
url=jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true

● 使用可视化客户端连接 MySQL 时出现 2058 错误

解决方案

MySQL 8.0 采用了 caching_sha2_password 加密,是 sha256 的改进版加密方式,多数第三方客户端都不支持这种加密方式,自带的命令行可支持。具体可参看 官方文档 有关该内容说明。要解决该问题,需要修改加密方式。以 root 用户为例,如果要配置其他用户或授权 IP,对应修改名称和地址即可。

1
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password';