Saturday, April 20, 2013

How to modify MAX_ALLOWED_PACKET in MySQL

While working with BLOB/CLOB fields in MySQL, it won't take long to encounter the following issue during a DML statement:

Packets larger than max_allowed_packet are not allowed.

Using the following command:

SHOW VARIABLES LIKE ‘max_allowed_packet’;

You can see that the networking parameter MAX_ALLOWED_PACKET defaults to 1048576 (1MB).  Since a 1MB BLOB file is fairly paltry, the parameter must be updated to a more reasonable value.  Logged in as root (or similarly admin'ed user), issue the following command to modify the parameter:

SET GLOBAL max_allowed_packet = 16 * 1024 * 1024;

GLOBAL refers to the sessions which are effected by the call; meaning that all sessions will now honor the new 16MB limit.  However, there is a catch.  At this point, the change is not persistent (see notes that this is not a bug).  A recycle of the service will reset the value back to the original 1MB setting.

To permanently change the value, the my.ini configuration file must be altered;  mileage may vary on the location of said file (install location, OS, etc., etc.), but in my case, the file lives in C:\Users\chris\Development\mysql\my.ini.  Find the mysqld section, and add a line for the setting:

[mysqld]
# ADDED BY CHRIS ON 20 APRIL 2013
max_allowed_packet=16M

Save the file and restart the MySQL service.  The value should now be permanent.

No comments:

Post a Comment