MySQL版本的相關問題:com.mysql.cj.jdbc.Driver和com.mysql.jdbc.Driver
1. 在使用mysql時,控制台日志報錯如下:
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.
報錯原因:
MySQL5用的驅動url是com.mysql.jdbc.Driver,MySQL6以后用的是com.mysql.cj.jdbc.Driver。版本不匹配便會報驅動類已過時的錯誤。
解決方法:
更改配置文件中的驅動類名字就可以消除驅動類過時的警告了。
2. JDBC連接MySQL6 (com.mysql.cj.jdbc.Driver), 需要指定時區serverTimezone,否則會報如下錯誤:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (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.)
3.還有一個警告:
WARN: 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連接。
如果未明確設置,MySQL 5.5.45+, 5.6.26+ and 5.7.6+版本默認要求建立SSL連接。
為了符合當前不使用SSL連接的應用程序,verifyServerCertificate屬性設置為’false’。
如果你不需要使用SSL連接,你需要通過設置useSSL=false來顯式禁用SSL連接。
如果你需要用SSL連接,就要為服務器證書驗證提供信任庫,並設置useSSL=true。
**/
名詞解釋:
SSL – Secure Sockets Layer(安全套接層)
所以,使用mysql6的連接字符串建議使用如下:
jdbc.url=jdbc:mysql://localhost:3306/your_database?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
//更詳細的連接字符創參考: mysql.url=jdbc:mysql://127.0.0.1:3306/dbname?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false&verifyServerCertificate=false&autoReconnct=true&autoReconnectForPools=true&allowMultiQueries=true
以上配置的寫法是寫在單獨的配置文件properties中,使用xml文件配置的寫法略有不同,需要將每一個配置用分號; 隔開,需要注意的是分號;在xml中需要轉義。XML文件中的寫法:
<property name="url" value="jdbc:mysql://localhost/lujx?serverTimezone=UTC&useSSL=false" />