mysql報錯Packet for query is too large (12238 > 1024). You can change this value


  今天將項目部署到linux服務器的時候莫名其妙的報一些錯誤,可是在本地啥錯沒有,通過實時查看tomcat 的日志之后發現報錯是:  

實時查看日志:

1、先切換到:cd usr/local/tomcat5/logs

2、tail -f catalina.out

3、這樣運行時就可以實時查看運行日志了

 

 

發現錯誤:

rg.springframework.dao.TransientDataAccessResourceException:
### Error querying database.  Cause: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1233 > 1024). You can change this value on the server by setting the max_allowed_packet' variable.
### The error may exist in cn/xm/exam/mapper/employee/out/custom/UnitCustomMapper.xml
### The error may involve cn.xm.exam.mapper.employee.out.custom.UnitCustomMapper.getHaulunitByCondition-Inline
### The error occurred while setting parameters
### SQL: SELECT   haulunit.unitId,   haulunit.unitBigId,   haulunit.bigId,   (select   group_concat(projectname) from project where projectid in (select projectId from   haulunitproject where bigId =haulunit.bigId and unitId=   haulunit.unitId)) as   projectNames,   haulunit.manager,   haulunit.managerPhone,   haulunit.secure,   haulunit.securePhone,   @b:=   (IFNULL((SELECT SUM(minusNum) FROM breakrules,   haulemployeeout WHERE   breakrules.BigEmployeeoutId =   haulemployeeout.BigEmployeeoutId    AND haulemployeeout.unitid =   haulunit.unitId           and breakTime LIKE CONCAT(Year(CurDate()),'%')   ),0)) AS unitMinisMum,   haulinfo.bigName,   haulinfo.bigStatus,   haulinfo.bigCreateDate,   unit.name,   unit.address,   unit.contact,   unit.phone,   @a:= (SELECT   COUNT(BigEmployeeoutId)   FROM haulemployeeout   WHERE   haulemployeeout.bigId   = haulunit.bigId   AND haulemployeeout.unitId =   haulunit.unitId and trainstatus='2') AS   personNum,   TRUNCATE(IFNULL(@b/@a,0),3) AS jiaquan   FROM haulunit,   haulinfo,   unit    WHERE haulunit.bigId=haulinfo.bigId AND    haulunit.unitId=unit.unitId                      and haulunit.bigId = ?     ORDER BY jiaquan desc               limit ?,?
### Cause: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1233 > 1024). You can change this value on the server by setting the max_allowed_packet' variable.
; SQL []; Packet for query is too large (1233 > 1024). You can change this value on the server by setting the max_allowed_packet' variable.; nested exception is com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1233 > 1024). You can change this value on the server by setting the max_allowed_packet' variable.
        at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:107)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)

 

 

 

-----------------解決辦法:(通過查看mysql全局變量發現原因是mysql限制了最大更新大小)----------------------

mysql max_allowed_packet 設置過小導致記錄寫入失敗

mysql根據配置文件會限制server接受的數據包大小。

有時候大的插入和更新會受max_allowed_packet 參數限制,導致寫入或者更新失敗。

查看目前配置

show VARIABLES like '%max_allowed_packet%';

顯示的結果為:

+--------------------+---------+

| Variable_name      | Value   |

+--------------------+---------+

| max_allowed_packet | 1048576 |

+--------------------+---------+  

以上說明目前的配置是:1M

 

修改方法

1、修改配置文件

可以編輯my.cnf來修改(windows下my.ini),在[mysqld]段或者mysql的server配置段進行修改。

max_allowed_packet = 20M

如果找不到my.cnf可以通過

mysql --help | grep my.cnf

去尋找my.cnf文件。

linux下該文件在/etc/下。

 

2、在mysql命令行中修改

在mysql 命令行中運行

set global max_allowed_packet = 2*1024*1024*10

然后退出命令行,重啟mysql服務,再進入。

show VARIABLES like '%max_allowed_packet%';

查看下max_allowed_packet是否編輯成功

 
 注意:該值設置過小將導致單個記錄超過限制后寫入數據庫失敗,且后續記錄寫入也將失敗。
 
 
三、如何重啟Linux的mysql

1、使用 service 啟動:service mysqld restart

2、使用 mysqld 腳本啟動:/etc/inint.d/mysqld restart

 


免責聲明!

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



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