注:这两天搭建项目时,使用Druid连接池入了不少坑;以此记录;
MySQL Server 5.7.21 + mysql-connector-java 5.1.38 + druid 1.0.8
一.配置步骤:
1.pom.xml 引入相关jar包:
2.spring.xml 数据源配置:
3.Druid监控配置这里没有写,数据库相关信息也可以写在config.properties资源文件里面;
二.注意事项:
1.MySQL Server 和 mysql-connector-java 要对应 ,如果是使用了 MySQL Server 8+和 mysql-connector-java8+ 版本;
&serverTimezone=时区标志" />
2. 报错:
java.sql.SQLException: Unknown system variable 'tx_isolation'
.com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
Caused by: java.lang.NullPointerException
at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:3299)
原因是MySQL Server 和 mysql-connector-java 版本不一致,使Druid驱动加载不能连接数据库;改成相应版本就行;如MySQL Server 5.7.21 + mysql-connector-java 5.1.38 + druid 1.0.8
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连接,禁用时在url上加上useSSL=false,如:
jdbc:mysql://localhost:3306/manangement?useUnicode=true&characterEncoding=utf8&useSSL=false
(如果在配置中发现其他错误,可以补充。谢谢。)
本文介绍使用Druid连接池时遇到的问题及解决方法,包括与MySQL的版本匹配、异常处理、SSL连接配置等关键信息。
1116

被折叠的 条评论
为什么被折叠?



