1、mysql 驱动版本问题
错误信息:
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.
解决方案:
使用了高版本的 mysql 驱动包后(6.0及以后), 对应的 driver 应该从 com.mysql.jdbc.Driver 变成 com.mysql.cj.jdbc.Driver 。
2、mysql 时区问题
错误信息:
Cause: 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.
永久解决方案:
1)修改 url 串,在后面添加 serverTimezone=UTC 属性即可,如:
jdbc.url=jdbc:mysql://localhost:3306/xxx?serverTimezone=UTC
2)修改 my.ini 配置文件
default-time_zone = '+8:00'
3、多表操作时,排序规则不一致问题
错误信息:
### Error querying database. Cause: java.sql.SQLException: Illegal mix of collations (utf8_swedish_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '='
### The error occurred while setting parameters
### Cause: java.sql.SQLException: Illegal mix of collations (utf8_swedish_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '='
; uncategorized SQLException for SQL []; SQL state [HY000]; error code [1267]; Illegal mix of collations (utf8_swedish_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '='; nested exception is java.sql.SQLException: Illegal mix of collations (utf8_swedish_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '='
解决方案:
上例中,用 a 表的字段与 b 表的字段作为查询条件对比时报错。
由错误信息可知 其中一个字段的排序规则为 utf8_swedish_ci 而另一个为 utf8_general_ci
将两个字段的排序规则统一即可。
4、插入数据时,主键或唯一键重复问题
错误信息:
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'xxx' for key 'xxxx'
### The error may involve xxxx.insert-Inline
### The error occurred while setting parameters
; SQL []; Duplicate entry 'xxx' for key 'xxx'; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'xxx' for key 'xxx'
解决方案:
避免使用重复的主键,可以设置成自增或者UUID等。
5、程序启动时报:Public Key Retrieval is not allowed
错误信息:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Public Key Retrieval is not allowed
解决方案:
在连接后面添加 allowPublicKeyRetrieval=true
6、1040-Too many connections
错误信息:
解决方案:
出现此情况可能是连接池配置量过小 。
找自己的MySQL安装目录,然后打开my.ini配置文件,找到 max_connections 属性,将后面的值调大即可。
如 max_connections=1000 ,然后重启一下mysql,即可。