ERROR 1153 (08S01): Got a packet bigger than 'max_allowed_packet'

Error Message

ERROR 1153 (08S01): Got a packet bigger than 'max_allowed_packet' bytes

Description

This error occurs when MySQL attempts to send or receive a data packet that exceeds the size limit defined by the max_allowed_packet system variable. MySQL uses packets for communication between the client and server, and this setting controls the maximum size of any single packet.

Causes

  • Inserting very large text, BLOB, or JSON values
  • Extremely long SQL queries or multi-queries
  • Queries returning massive amounts of data
  • Default max_allowed_packet value (often 4MB or 16MB) insufficient for needs
  • Different max_allowed_packet values between client and server
  • Importing large database dumps or CSV files
  • Transactions affecting many rows or large objects

Solutions

  1. Increase max_allowed_packet size:

    -- Check current max_allowed_packet value
    SHOW VARIABLES LIKE 'max_allowed_packet';
    
    -- Increase max_allowed_packet for current session
    SET GLOBAL max_allowed_packet = 1073741824; -- 1GB
    
    -- For permanent changes in my.cnf/my.ini:
    # [mysqld]
    # max_allowed_packet = 1G
  2. Configure client and server consistently:

    # Server configuration (my.cnf in server)
    [mysqld]
    max_allowed_packet = 256M
    
    # Client configuration (my.cnf in client)
    [mysql]
    max_allowed_packet = 256M
  3. Split large operations into smaller chunks:

    -- Instead of one large INSERT
    INSERT INTO large_table (blob_column) VALUES (HUGE_BLOB_VALUE);
    
    -- Use multiple smaller INSERTs
    -- Insert first chunk
    INSERT INTO large_table (id, blob_column) VALUES (1, CHUNK1);
    -- Insert second chunk
    INSERT INTO large_table (id, blob_column) VALUES (2, CHUNK2);
  4. Use external storage for very large objects:

    -- Store file path instead of content
    CREATE TABLE documents (
        id INT PRIMARY KEY,
        filename VARCHAR(255),
        file_path VARCHAR(1024),
        file_size BIGINT,
        upload_time TIMESTAMP
    );

Prevention

  1. Set appropriate packet size limits based on your application needs

  2. Design schemas to avoid very large rows:

    • Split large text into separate tables
    • Use external storage for binary data
  3. Monitor packet sizes:

    -- Enable query logging to identify large operations
    SET GLOBAL general_log = 'ON';
    SET GLOBAL log_output = 'TABLE';
  4. Use streaming APIs for large result sets in application code

Edit this page on GitHub