报错

Error querying database. Cause: com.mysql.cj.jdbc.exceptions.PacketTooBigException: Packet for query is too large (2,471 > 2,048). You can change this value on the server by setting the 'max_allowed_packet' variable. ### The error may exist in file [/Users/yangcong/Desktop/Java/health12/student-health/health-system/target/classes/mapper/system/NoticeMapper.xml] ### The error may involve com.health.system.mapper.NoticeMapper.selectNoticeList-Inline ### The error occurred while setting parameters ### SQL: select notice_id, notice_title, notice_type, cast(notice_content as char) as notice_content, status, create_by, create_time, update_by, update_time, remark from sys_notice LIMIT ? ### Cause: com.mysql.cj.jdbc.exceptions.PacketTooBigException: Packet for query is too large (2,471 > 2,048). You can change this value on the server by setting the 'max_allowed_packet' variable. ; Packet for query is too large (2,471 > 2,048). You can change this value on the server by setting the 'max_allowed_packet' variable.; nested exception is com.mysql.cj.jdbc.exceptions.PacketTooBigException: Packet for query is too large (2,471 > 2,048). You can change this value on the server by setting the 'max_allowed_packet' variable.


原因分析:

MySQL查询结果数据包太大,超过了服务器默认设置的 max_allowed_packet 限制(当前是2048字节)


解决方案:

查看正在进行的容器:

[root@cmqh etc]# docker ps
CONTAINER ID   IMAGE         COMMAND                   CREATED       STATUS       PORTS                                                           NAMES
4930f731f190   mysql:5.7     "docker-entrypoint.s…"   3 weeks ago   Up 2 days    0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp            mysql

方法1:永久解决方案(推荐)​

1、创建自定义配置文件

mkdir -p /etc/mysql/conf.d
cat <<EOF > /etc/mysql/conf.d/mysql_custom.cnf
[mysqld]
max_allowed_packet=64M
EOF

2、复制配置文件到容器

docker cp /etc/mysql/conf.d/mysql_custom.cnf mysql:/etc/mysql/conf.d/

3、重启 MySQL 容器

docker restart mysql

4、验证修改

docker exec -it mysql mysql -uroot -p -e "SHOW VARIABLES LIKE 'max_allowed_packet';"

方法2:临时解决方案(重启后失效)​

1、进入 MySQL 容器

docker exec -it mysql bash

2、登录 MySQL

mysql -uroot -p
# 输入密码后执行:

3、修改参数(临时生效)

SET GLOBAL max_allowed_packet=64 * 1024 * 1024;  -- 设置为64MB

4、验证修改

SHOW VARIABLES LIKE 'max_allowed_packet';

注意​​:此方法重启容器后会失效!

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐