SQLAlchemy and MySQL server has gone away (error 2006)

06 May 2013

I encountered a problem with MySQL and SQL alchemy when trying to run a large insert. Considering other inserts were working perfect it made me suspect that there was a tress hold in place which would not pass the large commit. I looked at the different settings and finally found out it was due to a too small maxallowedpacket value. I increased the value to 64M which solved my problem.

Depending if you are running Linux or Windows you should look for my.conf (Linux) or my.ini and edit the value maxallowedpacket. Look for the following section:

# The MySQL server
port        = 3306
socket      = /tmp/mysql.sock
key_buffer_size = 256M
max_allowed_packet = 64M   <======
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 8

You can also use the MySQL Workbench tool by clicking on the Home button on the top left, then opening the server administration window, click on the "Options File" file in the left menu-bar under configuration. Proceed by opening Networking tab and changing the value for maxallowedpacket and click the apply button. mysql Restart your MySQL server and profit.