连接mysql的时候被坑了好多次了,总结一下
1.以下是Mybatis连接mysql报的错
java">ERROR 01-23 19:01:57,762 Could not get a databaseId from dataSource (VendorDatabaseIdProvider.java:55)
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
... 50 more
最后排查发现是因为mysql的驱动版本太低
我换成了这个版本
我的mysql版本是8.0.15,所以JDBC驱动也要适配mysql的版本
2.时区有误差
java">ERROR 01-23 21:14:59,430 Could not get a databaseId from dataSource (VendorDatabaseIdProvider.java:55)
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.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
可以搜到很详细的错误原因,我就不赘述了,仅写解决方案
url的部分,在数据库的名字后面加上
java">jdbc:mysql://localhost:3306/companydb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT