Warning
Error Code:
1301
MySQL Error 1301: Max Packet Size Exceeded
Description
This error indicates that a result set from a function call or data being processed by MySQL has exceeded the configured `max_allowed_packet` size. When this limit is breached, data may be truncated, leading to incomplete results or unexpected behavior in your application.
Error Message
Result of %s() was larger than max_allowed_packet (%ld) - truncated
Known Causes
3 known causesTransferring Large Data Sets
Attempting to send or receive very large data (e.g., BLOB/TEXT fields, long strings, or many rows) in a single operation can exceed the packet limit.
Complex Queries or Stored Procedures
Queries that join many tables, return numerous columns, or stored procedures that generate extensive output can result in a packet size overflow.
Misconfigured `max_allowed_packet`
The MySQL server's `max_allowed_packet` variable might be set too low for the typical size of data or operations your application performs.
Solutions
3 solutions available1. Temporarily Increase max_allowed_packet for the Current Session easy
Quickly raise the limit for the current connection without server-wide changes.
1
Connect to your MySQL server using a client (e.g., `mysql` command-line tool, MySQL Workbench).
2
Execute the following SQL command to set the `max_allowed_packet` variable for the current session.
SET SESSION max_allowed_packet = 64 * 1024 * 1024; -- Set to 64MB, adjust as needed
3
Re-run the query or operation that caused the error. This change only affects your current connection.
2. Permanently Increase max_allowed_packet in MySQL Configuration medium
Modify the MySQL server's configuration file to permanently increase the limit for all connections.
1
Locate your MySQL configuration file. Common locations include `/etc/my.cnf`, `/etc/mysql/my.cnf`, or within the MySQL installation directory (e.g., `C:\ProgramData\MySQL\MySQL Server X.X\my.ini` on Windows).
2
Open the configuration file with a text editor (e.g., `nano`, `vim`, Notepad).
3
Add or modify the `max_allowed_packet` setting under the `[mysqld]` section. Choose a value that is sufficiently large for your needs (e.g., 64MB, 128MB, 256MB).
[mysqld]
max_allowed_packet = 64M
4
Save the configuration file.
5
Restart the MySQL server for the changes to take effect. The command to restart varies by operating system and installation method.
# On systems using systemd (e.g., Ubuntu 15.04+, CentOS 7+)
sudo systemctl restart mysql
# On systems using SysVinit (e.g., older Ubuntu/Debian, CentOS 6)
sudo service mysql restart
# On Windows
# Open Services.msc, find your MySQL service, and restart it.
3. Optimize Large Data Operations medium
Reduce the size of the data being sent or retrieved to avoid hitting the packet limit.
1
If you are inserting or updating large BLOB/TEXT data, consider if the entire data needs to be sent in a single operation. Can it be chunked or processed differently?
2
When retrieving large result sets, use `LIMIT` and `OFFSET` clauses to fetch data in smaller batches if the application logic allows. Alternatively, consider if you truly need to retrieve all columns (`SELECT *`) or if specific columns suffice.
SELECT column1, column2 FROM your_table WHERE ... LIMIT 1000 OFFSET 0;
SELECT column1, column2 FROM your_table WHERE ... LIMIT 1000 OFFSET 1000;
3
For bulk inserts of large amounts of data, consider using `LOAD DATA INFILE` which can be more efficient and may have different packet handling than individual `INSERT` statements.
LOAD DATA INFILE '/path/to/your/data.csv' INTO TABLE your_table FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
4
Review your application code. If it's constructing very large queries or processing large amounts of data before sending it to the database, refactor the application logic.