MySQL8.0相对于5.X的变化
1.驱动包版本发生变化
MySQL官方推荐使用 mysql-connector-java-8.X.jar 去连接 MySQL 8.0 的版本
Connector/J version | MySQL server version |
---|---|
5.1 | 5.6*, 5.7*, 8.0* |
8.0 | 5.6, 5.7, 8.0 |
2.com.mysql.jdbc.Driver 更换为 com.mysql.cj.jdbc.Driver
3.MySQL 8.0 以上版本不需要建立 SSL 连接的,需要显式关闭
MySQL 5.7 之前版本,安全性做的并不够好,比如安装时生成的root空密码账号、存在任何用户都能连接上的 test 库等,导致数据库存在较大的安全隐患。从5.7版本开始MySQL官方对这些问题逐步进行了修复,到了 MySQL 8.0 以上版本已经不需要使用 SSL 进行连接加密了。但是高版本仍然保留了这个接口,所以需要在连接的时候手动写明是否需要进行 SSL 连接,这里我们手动关闭SSL 连接加密。useSSL=false
4.设置 CST(设置时区)
serverTimezone=UTC
列如:
jdbc:mysql://localhost:3306/eip_gzjrj?useUnicode=true&useSSL=false&serverTimezone=UTC&characterEncoding=utf-8
遇到的问题
1.驱动器版本低于数据库版本
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection
升级驱动器版本
2.连接方式不对
java.sql.SQLNonTransientConnectionException: Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the connection string near ';characterEncoding=utf-8'.
升级驱动器版本后,连接方式需要改变
以前:
jdbc:mysql://localhost:3306/tree?useUnicode=true&characterEncoding=utf-8
现在:
jdbc:mysql://localhost:3306/tree?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = UTC