MySQL Packet for query is too large problem and solution

MySQL Packet for query is too large problem and solution

Problem description:

Error message:

Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1354 > 1024). You can change this value on the server
by setting the max_allowed_packet' variable

MySQL will limit the size of the data packet accepted by the Server according to the configuration file. Sometimes the size of the data packet during insert, update or query is limited by the max_allowed_packet parameter, causing the operation to fail.

View the max_allowed_packet parameter:

Execute on the client:

show VARIABLES like '%max_allowed_packet%';

The results are as follows:

+--------------------+------------+ 
| Variable_name | Value | 
+--------------------+-------------------+ 
| max_allowed_packet | 1024 | 
+--------------------+-------------------+

Modification method:

1. To modify the configuration file, you can edit my.cnf (my.ini under Windows) and modify it in the [mysqld] section or the mysql server configuration section.

max_allowed_packet = 20M

If my.cnf cannot be found, you can

mysql --help | grep my.cnf

Go find the my.cnf file.

In Linux, the file is located in /etc/.

2. Modify in the mysql command line

set global max_allowed_packet = 2*1024*1024*10  
//The value here is the number of bytes. If it is changed to 20M, you need to log in to the client again to take effect.

Note: After the client uses the command to modify, it will only take effect temporarily. After restarting MYSQL, the original value will be restored.

Solution to the error "Packet for query is too large (1040 > 1024)" reported by MySQL under Linux

The project had been running normally before, but suddenly in the past few days it kept prompting query errors. I looked at the logs and found that it prompted Packet for query is too large (1040 > 1024). You can change this value on the server by setting the max_allowed_packet' variable. error. I searched Baidu for the solution and recorded the solution process.

1. Reasons

The data packet returned by the query database is too large and exceeds the default value.
Log in to mysql and enter the following command in the console to query the default max_allowed_packet value. It is only 1024.

show VARIABLES like '%max_allowed_packet%';

2. Modify the default maximum allowed packet size

2.1 Method 1: Command method

(1). Enter the following command in the MySQL console to set max_allowed_packet to 20M

set global max_allowed_packet = 20*1024*1024;

(2) Exit MySQL, restart the MySQL service, and then log in to MySQL to check whether max_allowed_packet has been modified successfully.

show VARIABLES like '%max_allowed_packet%';

2.2 Method 2: Modify the configuration file my.cnf
(1) Enter the following command in the mysql console to edit my.cnf

sudo vi /etc/mysql/my.cnf

(2). Add below [mysqId]

max_allowed_packet = 20M

2.2 Exit edit mode and restart MySQL. Follow the steps in 2.1 (2)

3. Note

After configuration, I queried max_allowed_packet and found that it was 16777216, which is actually 16*1024*1024, not the expected 20M. So I checked again and found that the server memory capacity might be insufficient, because Java occupies a lot of it, so MySQL might automatically reset the parameters. So in some cases, it may be that after you changed the max_allowed_packet parameter, MySQL automatically reset the parameter to the default 1024 after a while, and the same error occurred again.

<<:  How to configure Nginx to split traffic based on the last segment of the request IP

>>:  Angular performance optimization: third-party components and lazy loading technology

Recommend

Detailed explanation of how to use zabbix to monitor oracle database

1. Overview Zabbix is ​​a very powerful and most ...

A brief discussion on ifnull() function similar to nvl() function in MySQL

IFNULL(expr1,expr2) If expr1 is not NULL, IFNULL(...

Implementation of Docker deployment of ElasticSearch and ElasticSearch-Head

This article mainly explains how to deploy Elasti...

Steps to enable TLS in Docker for secure configuration

Preface I had previously enabled Docker's 237...

Docker primary network port mapping configuration

Port Mapping Before the Docker container is start...

How to build your own Nexus private server in Linux

This article describes how to build a Nexus priva...

Use crontab to run the script of executing jar program regularly in centOS6

1. Write a simple Java program public class tests...

About installing python3.8 image in docker

Docker Hub official website 1. Search for Python ...

How to modify the length limit of group_concat in Mysql

In MySQL, there is a function called "group_...

Six inheritance methods in JS and their advantages and disadvantages

Table of contents Preface Prototype chain inherit...

Detailed explanation of Nginx's rewrite module

The rewrite module is the ngx_http_rewrite_module...

HTML table tag tutorial (34): row span attribute ROWSPAN

In a complex table structure, some cells span mul...

Example of converting spark rdd to dataframe and writing it into mysql

Dataframe is a new API introduced in Spark 1.3.0,...