报错信息概述
Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1078 > 1024). You can change this value on the server by setting the max_allowed_packet’ variable.
错误信息显示,查询包过大
错误原因
PacketTooBigException中packet的含义
A communication packet is a single SQL statement sent to the MySQL server, a single row that is sent to the client, or a binary log event sent from a master replication server to a slave.
packet可以是:
- 客户端发给服务器的sql statement
- 客户端接收到的结果集
- 主从数据库中的master发给slave的二进制日志事件
原因
结合上述packet的含义,可以得知其实就是以上几种数据包过大造成的错误。
如何解决
方法一:修改max_allowed_packet参数
Both the client and the server have their own max_allowed_packet variable, so if you want to handle big packets, you must increase this variable both in the client and in the server.
mysql客户端和服务器端都有各自的max_allowed_packet参数,注意同时修改
方法二:根据具体场景修改代码
1.Packet for query is too large
场景:sql过长导致的,尤其在动态拼接sql的时候,入参过多,会导致拼接的sql特别长
解决办法:拆分入参,多次查询
PS:需要注意的是每次遇到这种类似的常见,做好预判,提前做好限制