518

I'm importing a MySQL dump and getting the following error.

$ mysql foo < foo.sql 
ERROR 1153 (08S01) at line 96: Got a packet bigger than 'max_allowed_packet' bytes

Apparently there are attachments in the database, which makes for very large inserts.


This is on my local machine, a Mac with MySQL 5 installed from the MySQL package.

Where do I change max_allowed_packet to be able to import the dump?

Is there anything else I should set?

Just running mysql --max_allowed_packet=32M … resulted in the same error.

4
  • possible duplicate of How to change max_allowed_packet size Jan 17, 2014 at 14:19
  • 3
    @Muleskinner, this question was posted 3 years before the one you mention and I'm pointing this out 4 years after your comment. :p
    – tiomno
    Feb 20, 2018 at 5:41
  • 2
    Webyog.com Link is broken: 404
    – Pathros
    Feb 26, 2018 at 19:40
  • Here, a similar error, "Packet for query is to large (5526600 > 1048576).", was caused by a wrong password entry, when connecting to MySQL database using DataGrip 2016.1 by JetBrains, and got resolved when rectified (using the right password corresponding to the MySQL database user). Mar 24, 2018 at 13:33

15 Answers 15

732

You probably have to change it for both the client (you are running to do the import) AND the daemon mysqld that is running and accepting the import.

For the client, you can specify it on the command line:

mysql --max_allowed_packet=100M -u root -p database < dump.sql

Also, change the my.cnf or my.ini file (usually found in /etc/mysql/) under the mysqld section and set:

max_allowed_packet=100M

or you could run these commands in a MySQL console connected to that same server:

set global net_buffer_length=1000000; 
set global max_allowed_packet=1000000000;

(Use a very large value for the packet size.)

18
  • 16
    FYI - it helped me to solve a DIFFERENT error - "#2006 server has gone away"
    – itsho
    Dec 13, 2011 at 16:05
  • 51
    Be aware that using "set global" works until the next mysql service restart. Nov 16, 2012 at 21:28
  • 3
    Skip "set global" and the final ";" when adding these values to the my.ini or my.cnf files. Ex: "net_buffer_length=1000000" in my.conf.
    – Rustavore
    May 28, 2014 at 23:52
  • 5
    On CentOS 5, my.cnf is located at /etc/my.cnf
    – Rustavore
    May 28, 2014 at 23:53
  • 2
    For those using PHPMyAdmin I was able to do this technique by starting on the home page of PHPMyAdmin, then clicking the variables tab, then you can search for 'packet' or 'buffer' to narrow down and edit from there.
    – ajk4550
    Jun 18, 2015 at 18:25
140

As michaelpryor said, you have to change it for both the client and the daemon mysqld server.

His solution for the client command-line is good, but the ini files don't always do the trick, depending on configuration.

So, open a terminal, type mysql to get a mysql prompt, and issue these commands:

set global net_buffer_length=1000000; 
set global max_allowed_packet=1000000000; 

Keep the mysql prompt open, and run your command-line SQL execution on a second terminal..

1
  • 2
    Solved the problem for me; the import I'm doing is a once-off, and I can't easily change the configuration. This worked great. :D
    – Rob Howard
    Nov 26, 2009 at 4:11
49

This can be changed in your my.ini file (on Windows, located in \Program Files\MySQL\MySQL Server) under the server section, for example:

[mysqld]

max_allowed_packet = 10M
4
  • 5
    on a mac, file obviously located elsewhere.
    – kch
    Sep 18, 2008 at 14:44
  • 2
    sure, but the configuration is still somewhere although I don't know the exact location
    – GHad
    Sep 18, 2008 at 17:15
  • 1
    For me in Fedora 20 with MariaDB, placing that setting at the end of /etc/my.cnf.d/server.cnf did the trick. I had to restart the service of course... sudo nano systemctl restart mariadb.service
    – Ray Foss
    Aug 11, 2014 at 19:54
  • The file would more likely be "my.cnf" and on nix systems, typically in /etc/ or /usr/local/etc. Once you edit, be sure to restart mysql server to apply change.
    – Chris
    Jan 30, 2019 at 22:31
18

Re my.cnf on Mac OS X when using MySQL from the mysql.com dmg package distribution

By default, my.cnf is nowhere to be found.

You need to copy one of /usr/local/mysql/support-files/my*.cnf to /etc/my.cnf and restart mysqld. (Which you can do in the MySQL preference pane if you installed it.)

2
  • The default config in place for OSX appears to be my-medium.cnf, although the max_allowed_packet size is the same in my-large.cnf ... until you start changing things :) Aug 1, 2011 at 0:15
  • In my case /usrl/local/mysql/my.cnf not worked until I copy it to /etc/my.cnf.
    – V G
    Aug 13, 2015 at 14:50
17

The fix is to increase the MySQL daemon’s max_allowed_packet. You can do this to a running daemon by logging in as Super and running the following commands.

# mysql -u admin -p

mysql> set global net_buffer_length=1000000;
Query OK, 0 rows affected (0.00 sec)

mysql> set global max_allowed_packet=1000000000;
Query OK, 0 rows affected (0.00 sec)

Then to import your dump:

gunzip < dump.sql.gz | mysql -u admin -p database
2
  • What version of MySQL were you running this on?
    – crmpicco
    Oct 7, 2015 at 12:17
  • This one works for my server. Thanks
    – Ken Lee
    Jun 26, 2022 at 1:33
16

In etc/my.cnf try changing the max_allowed _packet and net_buffer_length to

max_allowed_packet=100000000
net_buffer_length=1000000 

if this is not working then try changing to

max_allowed_packet=100M
net_buffer_length=100K 
7

On CENTOS 6 /etc/my.cnf , under [mysqld] section the correct syntax is:

[mysqld]
# added to avoid err "Got a packet bigger than 'max_allowed_packet' bytes"
#
net_buffer_length=1000000 
max_allowed_packet=1000000000
#
6

I have resolved my issue by this query

SET GLOBAL max_allowed_packet=1073741824;

and check max_allowed_packet with this query

SHOW VARIABLES LIKE 'max_allowed_packet';
5

Use a max_allowed_packet variable issuing a command like

mysql --max_allowed_packet=32M -u root -p database < dump.sql

1
  • 2
    tried that, didn't work. whole dump in 272mb, tried with max higher than that.
    – kch
    Sep 18, 2008 at 14:46
5

Slightly unrelated to your problem, so here's one for Google.

If you didn't mysqldump the SQL, it might be that your SQL is broken.

I just got this error by accidentally having an unclosed string literal in my code. Sloppy fingers happen.

That's a fantastic error message to get for a runaway string, thanks for that MySQL!

2
  • 1
    I also got this error because of broken SQL. More specifically, my table has null constraints and my code was INSERTing null values. Instead of giving me an informative error, MySQL returned the max_allowed_packet error. If it helps for those in the future, I was INSERTing using the pandas API df.to_sql(...) Nov 14, 2016 at 19:42
  • Something similar happened to me (and I did use mysqldump. Fortunately my IDE highlighting caught it once I knew to look for the problem. Thank you.
    – Erica Kane
    Jan 30 at 16:36
3

Error:

ERROR 1153 (08S01) at line 6772: Got a packet bigger than 'max_allowed_packet' bytes Operation failed with exitcode 1

QUERY:

SET GLOBAL max_allowed_packet=1073741824;
SHOW VARIABLES LIKE 'max_allowed_packet'; 

Max value:

Default Value (MySQL >= 8.0.3)  67108864
Default Value (MySQL <= 8.0.2)  4194304
Minimum Value   1024
Maximum Value   1073741824
2

Sometimes type setting:

max_allowed_packet = 16M

in my.ini is not working.

Try to determine the my.ini as follows:

set-variable = max_allowed_packet = 32M

or

set-variable = max_allowed_packet = 1000000000

Then restart the server:

/etc/init.d/mysql restart
2

It is a security risk to have max_allowed_packet at higher value, as an attacker can push bigger sized packets and crash the system.

So, Optimum Value of max_allowed_packet to be tuned and tested.

It is to better to change when required (using set global max_allowed_packet = xxx) than to have it as part of my.ini or my.conf.

1

I am working in a shared hosting environment and I have hosted a website based on Drupal. I cannot edit the my.ini file or my.conf file too.

So, I deleted all the tables which were related to Cache and hence I could resolve this issue. Still I am looking for a perfect solution / way to handle this problem.

Edit - Deleting the tables created problems for me, coz Drupal was expecting that these tables should be existing. So I emptied the contents of these tables which solved the problem.

0

Set max_allowed_packet to the same (or more) than what it was when you dumped it with mysqldump. If you can't do that, make the dump again with a smaller value.

That is, assuming you dumped it with mysqldump. If you used some other tool, you're on your own.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.