Springboot + MySql 连接常见问题及解决方案

一、驱动版本

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.6</version>
</dependency>
version驱动类springboot 版本
5 +com.mysql.jdbc.Driverspringboot 2.0.3-
6 +com.mysql.cj.jdbc.Driverspringboot 2.0.4+

二、常见异常

1、指定是否使用 SSL

useSSL=false

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的现有应用程序,verifyServerCertificate属性设置为“false”。
您需要通过设置useSSL = false显式禁用SSL,或者设置useSSL = true启用SSL并为服务器证书验证提供信任库。

2、指明时区

serverTimezone=Asia/Shanghai 或者 serverTimezone=GMT%2B8

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.
---------
服务器时区值' й      '无法识别或代表多个时区。
如果你想利用时区支持,您必须通过 serverTimezone 配置属性配置服务器或JDBC驱动程序使用更具体的时区值

3、设置数据库编码

useUnicode=true&characterEncoding=utf8


4、TIMESTAMP 0 值转换问题

zeroDateTimeBehavior=convertToNull

java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 7 to TIMESTAMP

该属性有下列三个属性值:

  1. exception 默认值,即抛出 SQL state [S1009]. Cannot convert value.... 的异常
  2. convertToNull 将日期转换成 NULL
  3. round 替换成最近的日期即 0001-01-01

5、批量插入性能问题

rewriteBatchedStatements=true

  • 默认关闭
  • 大量数据批量插入时,会出现入库比较慢的问题,这时可以通过指定 MySQL 驱动参数,进而显著提升批量入库性能

6、单个连接中执行多个 SQL 语句(包括批量插入)

allowMultiQueries=true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值