1. 程式人生 > >Mysql版本的相關問題:com.mysql.cj.jdbc.Driver和com.mysql.jdbc.Driver

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。版本不匹配便會報驅動類已過時的錯誤。

?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false

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.)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.

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 
ps:以上配置的寫法是寫在單獨的配置檔案properties中,使用xml檔案配置的寫法略有不同,需要將每一個配置用分號; 隔開,需要注意的是分號;在xml中需要轉義,例如

properties檔案中配置寫法

dbc.url=jdbc:mysql://localhost:3306/your_database?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false

xml檔案中的配置寫法

<property name="url" value="jdbc:mysql://localhost/lujx?serverTimezone=UTC&amp;useSSL=false" />