今天用java写了批量插入运行时,报错:
Error updating database. Cause: com.mysql.cj.jdbc.exceptions.PacketTooBigException: Packet for query is too large (1,399,241 > 1,048,576). You can change this value on the server by setting the 'max_allowed_packet' variable.
出现问题的原因:批量插入数据量过大
MySQL根据配置文件会限制Server接受的数据包大小。有时候插入、更新或查询时数据包的大小,会受 max_allowed_packet 参数限制,导致操作失败。
mysql> show VARIABLES like '%max_allowed_packet%';
+--------------------------+------------+
| Variable_name | Value |
+--------------------------+------------+
| max_allowed_packet | 1048576 |
| slave_max_allowed_packet | 1073741824 |
+--------------------------+------------+
说明:
1*1024*1024 = 1048576 =1M
解决办法
修改mysql配置文件 windows下 my.ini
在[mysqld]下添加
max_allowed_packet = 524288000
说明:
500*1024*1024 =524288000=500M
修改完成之后要重启mysql
mysql> show VARIABLES like '%max_allowed_packet%';
+--------------------------+------------+
| Variable_name | Value |
+--------------------------+------------+
| max_allowed_packet | 524288000 |
| slave_max_allowed_packet | 1073741824 |
+--------------------------+------------+