1、mysql - url 參數解析
url:jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=utf8
useUnicode、characterEncoding 添加的作用是:指定字符的編碼、解碼格式。
例如:mysql數據庫用的是gbk編碼,而項目數據庫用的是utf-8編碼。這時候如果添加了useUnicode=true&characterEncoding=UTF8 ,那么作用有如下兩個方面:
1)存數據時
數據庫在存放項目數據的時候會先用UTF8格式將數據解碼成字節碼,然后再將解碼后的字節碼重新使用GBK編碼存放到數據庫中。
2)取數據時
在從數據庫中取數據的時候,數據庫會先將數據庫中的數據按GBK格式解碼成字節碼,然后再將解碼后的字節碼重新按UTF8格式編碼數據,最后再將數據返回給客戶端。
注意:在xml配置文件中配置數據庫utl時,要使用&的轉義字符也就是&
例:
<property name="url" value="jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=UTF8" />
2、ssl 報錯
web應用中連接mysql數據庫時后台會出現這樣的提示:
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連接。根據MySQL5.5.45+、5.6.26+和5.7.6+的要求,如果沒有設置顯式選項,那么缺省情況下必須建立SSL連接。以符合現有應用程序。不使用SSL,VerifyServerCertificate屬性設置為“false”。您需要通過設置usessl=false顯式禁用ssl,或者設置usessl=true並為服務器證書驗證提供信任庫。
原因:MySQL在高版本需要指明是否進行SSL連接。解決方案:在mysql連接字符串url中加入ssl=true或者false即可,如下:
url:jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=utf8&useSSL=false