【MySql】數據庫連接異常:Thelastpacketsentsuccessfullytotheserverwas0millisecondsago


參考鏈接:http://blog.sina.com.cn/s/blog_7540bf5f0102xjpk.html

最近新入職,用了新版的mysql8數據庫,結果連接數據庫時出現了問題,報了幾個異常,


1:第一個異常一看就知道是驅動包版本不對,於是就換了成mysql-connector-java-8.0.11  的驅動,就可以了。
Cannot connect to database server,Exception:com.mysql.cj.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
at
at
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.
at
at
at
at com.yascn.test.ConnectionTest.main(ConnectionTest.
第二個異常:
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
 
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.
at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.
at
at
at com.yascn.test.ConnectionTest.main(ConnectionTest.
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
 

網上說的什么修改的方法對我沒有用

諸如此類:

 

1·修改my.ini bind-address = 127.0.0.1 為 bind-address = 0.0.0.0   使非本地網絡可以訪問數據庫

2·檢查防火牆是否有開放3306端口

3·網上廣為流傳的86400秒超時 回收機制 並不和我遇到的問題一致,這種問題是在程序運行過程中因為我們使用的連接池不知道連接被回收了所以報出的異常,解決方案可以是修改連接池配置或修改mysql空閑超時時間配置。

於是按照說明更改了,找到 C:\ProgramData\MySQL\MySQL Server 8.0\my.ini文件新增
wait_timeout=31536000  
interactive_timeout=31536000 
 
但是都不管用,我就是想是否還是驅動的問題,看到了一版新的帖子,說是useSSL=false就可以了。

Class.forName("com.mysql.cj.jdbc.Driver");

url = "jdbc:mysql://localhost:3306/jnditest?serverTimezone=GMT&useSSL=false";//數據庫的路徑

主要是紅色字體方面,serverTimezone缺失是很容易從錯誤找出來的,但是重點是useSSL=false,這個從錯誤提示是看不出來的,它有個很重要的作用是使用JDBC跟你的數據庫連接的時候,你的JDBC版本與MySQL版本不兼容,MySQL的版本更高一些,在連接語句后加上“useSSL=‘true’” ,就可以連接到數據庫了。更高版本。但如果是兼容版本的話就會出錯。

我試了改成true的確又出現了這個問題。

查下了useSSL 的意思,是說JDBC版本與MySQL版本是否需要兼容,為true的是需要兼容,flase不需要兼容。

引用地址:https://blog.csdn.net/ponxbin/article/details/80608040

回顧下mysql連接url參數設置,與數據庫連接中斷重新連接時,需要在url中加上autoReconnect=true

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE
jdbc.username=root
jdbc.password=password

 

database.url=jdbc:mysql://database_server:3306/oauth?autoReconnect=true&useUnicode=true&createDatabaseIfNotExist=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC

 

 

特別說明其中的jdbc.url配置:如果你已經升級好了mysql-connector,其中的characterEncoding=utf8可以被自動被識別為utf8mb4(當然也兼容原來的utf8),而autoReconnect配置我強烈建議配上,我之前就是忽略了這個屬性,導致因為緩存緣故,沒有讀取到DB最新配置,導致一直無法使用utf8mb4字符集,多么痛的領悟!!

沒有讓mysql驅動開啟批量執行sql的開關。

怎么開啟呢?在拼裝mysql鏈接的url時,為其加上allowMultiQueries參數,設置為true,如下:

jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/database?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true

 

mysql JDBC URL格式如下: 

jdbc:mysql://[host:port],[host:port].../[database][?參數名1][=參數值1][&參數名2][=參數值2]... 

常用的幾個較為重要的參數: 

參數名稱 參數說明 缺省值 最低版本要求 
user 數據庫用戶名(用於連接數據庫) 所有版本 
passWord 用戶密碼(用於連接數據庫) 所有版本 
useUnicode 是否使用Unicode字符集,如果參數characterEncoding設置為gb2312或gbk,本參數值必須設置為true false 1.1g 
characterEncoding 當useUnicode設置為true時,指定字符編碼。比如可設置為gb2312或gbk false 1.1g 
autoReconnect 當數據庫連接異常中斷時,是否自動重新連接? false 1.1 
autoReconnectForPools 是否使用針對數據庫連接池的重連策略 false 3.1.3 
failOverReadOnly 自動重連成功后,連接是否設置為只讀? true 3.0.12 
maxReconnects autoReconnect設置為true時,重試連接的次數 3 1.1 
initialTimeout autoReconnect設置為true時,兩次重連之間的時間間隔,單位:秒 2 1.1 
connectTimeout 和數據庫服務器建立socket連接時的超時,單位:毫秒。 0表示永不超時,適用於JDK 1.4及更高版本 0 3.0.1 
socketTimeout socket操作(讀寫)超時,單位:毫秒。 0表示永不超時 0 3.0.1 

對應中文環境,通常mysql連接URL可以設置為: 

復制代碼代碼如下:

jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false 


在使用數據庫連接池的情況下,最好設置如下兩個參數: 

復制代碼代碼如下:

autoReconnect=true&failOverReadOnly=false 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM