Saturday, April 4, 2009

How to increase max_allowed_packet size in MySQL database

The max_allowed_packet size of MySQL is 1 M, which will be easy to raise an error when you try to upload (save) a big file in a MySQL database. The error message looks like this:

Mysql::Error: Got a packet bigger than 'max_allowed_packet' bytes:

How to modify the max_allowed_packet size, it is very easy:

1. run mysql shell:

mysql> SHOW VARIABLES LIKE 'max_allowed_packet';

+--------------------+-----------------+
| Variable_name | Value |
+--------------------+------------------+
| max_allowed_packet | 1048576 |
+--------------------+-------------------+
1 row in set (0.00 sec)

2. change the max allowed packet size:

edit the /etc/my.cnf file, add the value as the following before the line [mysql_safe]:

max_allowed_packet = 20M

save /etc/my.cnf file, restart the mysqld service

3. Verify the max_allowed_packet:

mysql> SHOW VARIABLES LIKE 'max_allowed_packet';
+--------------------+---------------------+
| Variable_name | Value |
+--------------------+---------------------+
| max_allowed_packet | 20970496 |
+--------------------+---------------------+
1 row in set (0.00 sec)

Now the max_allowed_packet size is changed to 20 M.

1 comment:

  1. Hello, I was having some problems with max_allowed_packets and found this article. Thank you for the fix and I just wanted to let you know this article was well written and easy to follow.

    ReplyDelete