MySQL-Java项目中的配置参数说明

useSSL

异常信息

在SpringBoot项目中连接MySQL数据库出现如下报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
Caused by: javax.net.ssl.SSLException: Received fatal alert: internal_error
at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:2020)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1127)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1367)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1395)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1379)
at com.mysql.cj.protocol.ExportControlled.performTlsHandshake(ExportControlled.java:316)
at com.mysql.cj.protocol.StandardSocketFactory.performTlsHandshake(StandardSocketFactory.java:188)
at com.mysql.cj.protocol.a.NativeSocketConnection.performTlsHandshake(NativeSocketConnection.java:99)
at com.mysql.cj.protocol.a.NativeProtocol.negotiateSSLConnection(NativeProtocol.java:352)
... 13 more

有人在使用 mysql-jdbc 连接 MySQL 遇到了如下警告:

Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+,5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

这两个异常是同一个原因导致的,根据上面的告警内容,翻译过来大概就是:
不建议在没有服务器的身份认证情况下建立SSL连接。根据 MySQL 5.5.45+、5.6.26+ 和 5.7.6+ 这些版本的要求,如果没有显式地设置选项,则默认必须建立SSL连接。为了兼容那些未使用SSL的已有应用服务,你需要通过显式设置useSSL=false来显式禁用SSL,或者设置useSSL=true并为服务器证书验证提供信任存储。

useSSL是什么?

useSSL 是 MySQL JDBC 驱动程序中的一个配置参数,表示是否在客户端与 MySQL 数据库之间使用SSL协议进行通信,该参数可以设置为true或false。
SSL协议提供服务主要:
1)认证用户服务器,确保数据发送到正确的服务器; .
2)加密数据,防止数据传输途中被窃取使用;
3)维护数据完整性,验证数据在传输过程中是否丢失;

解决方案

对于本地开发环境,通常我们不会配置服务器证书验证。所以对于SpringBoot项目中的这个报错,可以在 url 上加上useSSL=false来解决。

1
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test_db?characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false

serverTimezone

异常信息

SpringBoot项目连接MySQL数据库报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: 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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132)
at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2243)
at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2267)
at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1319)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:966)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
... 6 more

原因

MySQL驱动程序版本的问题,spring boot自动导入的 mysql-connector-java 是8.x版本,需要显式配置serverTimezone参数。

解决方案

增加serverTimezone参数。在jdbc连接的url后面加上serverTimezone=UTC即可解决问题,如果需要使用GMT+8时区,需要写成 GMT%2B8,或者也可配成 Asia/Shanghai

1
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test_db?characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false

也可以手动选择低版本,如5.x版本,就不会存在时区问题。

1
2
3
4
5
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>

------ 本文完 ------