1. 程式人生 > >mysql max_allowed_packet 設定

mysql max_allowed_packet 設定

  mysql5資料,插入或更新欄位有大資料時(大於1M),會出現如下錯誤: SEVERE: Servlet.service() for servlet [webs] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.TransientDataAccessResourceException: Hibernate flushing: Could not execute JDBC batch update; SQL [update t_article set (省略部分...)author_id=? where id=?]; 
Packet for query is too large (1117260 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.; nested exception is java.sql.BatchUpdateException: Packet for query is too large (1117260 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.] with root cause java.sql.BatchUpdateException: Packet for query is too large (1117260 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.

開始以為是相應欄位的型別設定不正確,超出了欄位的最大範圍,網上查詢後發現是mysql預設載入的資料檔案不超過1M,可以通過更改mysql的配置檔案my.cnf(Linux,或windows的my.ini)來更改這一預設值,從而達到插入大資料的目的。

解決: MySQL的一個系統引數:max_allowed_packet,其預設值為1048576(1M), 可以先查詢一下:show VARIABLES like '%max_allowed_packet%'; 修改此變數的值:MySQL安裝目錄下的my.ini檔案中的[mysqld]段中的"max_allowed_packet = 1M",如更改為4M(如果沒有這行內容,增加一行),儲存,重新啟動MySQL服務。現在可以load大於1M的檔案了。
有時候命令“
SET GLOBAL max_allowed_packet=16*1024*1024”無效,需要使用如下命令: set @@max_allowed_packet=5*1024*1024