Skip to content

Packets larger than max_allowed_packet are not allowed

adriancs edited this page Jul 4, 2025 · 16 revisions

Handling MySQL "max_allowed_packet" Limitations

Overview

The max_allowed_packet system variable in MySQL defines the maximum size of packets that can be sent to or received from the MySQL server. When a SQL query exceeds this limit, MySQL will reject the packet and return an error message:

Packets larger than "max_allowed_packet" are not allowed

Understanding Packet Size Limits

MySQL queries vary significantly in size depending on their complexity and data volume. A simple query like:

SELECT * FROM member;

contains only 21 characters. However, queries involving large data insertions, such as bulk INSERT operations with LONGBLOB or LONGTEXT data, can quickly exceed the default packet size limit.

Example of a bulk insert operation:

INSERT INTO member(code,name,age) VALUES
('A1','John',30),
('B1','Smith',32),
('C1','Adam',31),
('D1','Cane',28),
('E1','Irene',25),
('F1','Boo',21);

While the above query contains 144 characters, combining multiple large INSERT statements can easily reach the max_allowed_packet limit.

Default Value: The default max_allowed_packet value is 64MB (67,108,864 bytes) in MySQL 8.0 and later versions. Earlier versions of MySQL had lower default values (typically 1MB or 16MB).

Solution Methods

Method 1: Dynamic SQL Configuration

You can adjust the max_allowed_packet value using SQL commands during runtime.

Set limit to 128MB:

SET GLOBAL max_allowed_packet=128*1024*1024;

Set limit to 1GB (maximum allowed value):

SET GLOBAL max_allowed_packet=1024*1024*1024;

For session-specific changes:

SET SESSION max_allowed_packet=128*1024*1024;

Important considerations:

  • The database user must have administrative privileges to modify GLOBAL variables
  • SET GLOBAL changes apply to new connections, not the current session
  • SET SESSION changes apply only to the current connection
  • Changes are temporary and reset when the MySQL server restarts

C# Implementation Example:

using (var conn = new MySqlConnection(connectionString))
using (var cmd = conn.CreateCommand())
{
    conn.Open();
    
    cmd.CommandText = "SET SESSION max_allowed_packet=128*1024*1024;";
    cmd.ExecuteNonQuery();
    
    // Configuration is now active for this session
    // Proceed with operations requiring larger packet sizes
}

Method 2: MySQL Configuration File Modification

For permanent configuration changes, modify the MySQL configuration file.

Steps:

  1. Stop the MySQL server service
  2. Locate the MySQL configuration file:
    • Windows: %ProgramData%\MySQL\MySQL Server x.x\my.ini or C:\ProgramData\MySQL\MySQL Server x.x\my.ini
    • Unix/Linux: /etc/mysql/my.cnf
  3. Add or modify the following line under the [mysqld] section:
    max_allowed_packet=128M
  4. Restart the MySQL server service

This method provides a permanent solution that persists across server restarts.

Reference: [MySQL Configuration Files Documentation](https://dev.mysql.com/doc/refman/8.0/en/option-files.html)

Method 3: MySqlBackup.NET Library Configuration

When administrative access to MySQL server settings is not available, you can limit the maximum SQL query length generated by MySqlBackup.NET using the MaxSqlLength property.

Example implementation:

using (var conn = new MySqlConnection(connectionString))
using (var cmd = conn.CreateCommand())
using (var mb = new MySqlBackup(cmd))
{
    conn.Open();
    mb.ExportInfo.MaxSqlLength = 64 * 1024 * 1024; // 64MB limit
    mb.ExportToFile(filePath);
}

Important note: When working with tables containing large data types (MEDIUMTEXT, LONGTEXT, MEDIUMBLOB, LONGBLOB), this approach may still encounter limitations if individual records exceed the server's max_allowed_packet setting during import operations.

Determining Current Packet Size Limits

To check the current max_allowed_packet value on your MySQL server, execute:

SHOW VARIABLES LIKE 'max_allowed_packet';

This query will display the current maximum packet size configured on the server.

Administrative Responsibilities

The max_allowed_packet value is typically managed by the MySQL server administrator or database owner. If you encounter packet size limitations and cannot modify the server configuration, consult with your database administrator to determine the appropriate solution for your specific use case.

Additional Resources

For comprehensive information about MySQL packet size limitations and troubleshooting, refer to the official MySQL documentation:

Clone this wiki locally