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