MySQLNonTransientConnectionException: Could not create connection to database server.
Spring整合mybatis并使用driud数据库连接池,启动测试类就报”MySQLNonTransientConnectionException: Could not create connection to database server.“,检查了一番配置文件没发现问题,而且我之前用过这些配置,按理说没问题。各种百度、谷歌没结果,想起来装了mysql 8.0,估计是这个问题。既然是这个问题,那么问题出在数据库连接驱动,看了下pom文件,发现用的是5.1.4版本,于是找了个8.0.11版本重新启动,果然报错就不一样了。
2018-05-26 16:52:53,928 [Druid-ConnectionPool-Create-1150963491] [com.alibaba.druid.pool.DruidDataSource]-[ERROR] create connection error
java.sql.SQLException: validateConnection false
另外在控制台上还打印了”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 8.0使用的数据库连接字符串不一样。改完后重新启动搞定。
需要说明的是mysql 8.0版本还对时区有要求,否则启动提示”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.“
有个别人的示例可以参考下:https://blog.csdn.net/beyond9305/article/details/80330125
贴一下连接字符串
url: jdbc:mysql://localhost:3306/yinliu?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
driver: com.mysql.cj.jdbc.Driver