# MySQL Error inserting blob

**URL:** <https://www.ruby-forum.com/t/mysql-error-inserting-blob/155290>\
**Category:** Rails\
**Created:** [December 10, 2008, 9:04am UTC](https://www.ruby-forum.com/t/mysql-error-inserting-blob/155290 "2008-12-10T09:04:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![farmerss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@farmerss](https://www.ruby-forum.com/u/farmerss)\
**Post date:** [December 10, 2008, 9:04am UTC](https://www.ruby-forum.com/t/mysql-error-inserting-blob/155290/1 "2008-12-10T09:04:25Z")

</div>

Hi,

I’m trying to insert a blob into a MySQL database with a Rails app. I’m  
getting the error:

ActiveRecord::StatementInvalid (Mysql::Error: MySQL server has gone  
away: INSERT INTO `index_entries`  
(`legal_case_id`,`document_id`,`sword`,`word_count`,`map_data`,`created_at`,`updated_at`)  
VALUES (3,1368,‘f’,208720,‘\0\0…blob data…\0Å’1\0  
Å’1\0’,‘2008-12-10 05:17:29’,‘2008-12-10 05:17:29’) ON DUPLICATE KEY  
UPDATE `index_entries`.`updated_at`=VALUES(`updated_at`)):

I believe the blob itself should be about 820K long. Cutting it back to  
800K, it works alright. But I’ve checked my MySQL config, and it claims  
that max\_packet\_size=1M. I’ve also tried setting it to 32M, and that  
didn’t make any difference. I’ve also tried replacing the data with a  
sequence “01010101…” of the same length, and the same thing occurs: at  
800K it works, but the same error occurs at 820K. Does anyone know  
what’s going on?

Any help most appreciated!

Here is the stack trace:

```
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:188:in

```

`log' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/mysql_adapter.rb:309:in`execute’  
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active\_record/connection\_adapters/abstract/database\_statements.rb:171:in  
`insert_sql' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/mysql_adapter.rb:319:in`insert\_sql’  
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active\_record/connection\_adapters/abstract/database\_statements.rb:44:in  
`insert_without_query_dirty' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/abstract/query_cache.rb:18:in`insert’  
C:/Ruby/lib/ruby/gems/1.8/gems/ar-extensions-0.8.0/lib/ar-extensions/adapters/abstract\_adapter.rb:46:in  
`insert_many' C:/Ruby/lib/ruby/gems/1.8/gems/ar-extensions-0.8.0/lib/ar-extensions/adapters/abstract_adapter.rb:43:in`each’  
C:/Ruby/lib/ruby/gems/1.8/gems/ar-extensions-0.8.0/lib/ar-extensions/adapters/abstract\_adapter.rb:43:in  
`insert_many' C:/Ruby/lib/ruby/gems/1.8/gems/ar-extensions-0.8.0/lib/ar-extensions/import.rb:282:in`import\_without\_validations\_or\_callbacks’  
C:/Ruby/lib/ruby/gems/1.8/gems/ar-extensions-0.8.0/lib/ar-extensions/import.rb:243:in  
`import_with_validations' C:/Ruby/lib/ruby/gems/1.8/gems/ar-extensions-0.8.0/lib/ar-extensions/import.rb:202:in`import’  
/app/models/document.rb:150:in `index' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/buffered_logger.rb:25:in`silence’  
/app/models/document.rb:149:in `index' /app/models/document.rb:19:in`integrate\_import\_record’  
/app/models/import\_set.rb:44:in `integrate' /app/models/import_set.rb:33:in`each’  
/app/models/import\_set.rb:33:in `integrate' /app/controllers/import_sets_controller.rb:47:in`integrate’  
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action\_controller/base.rb:1253:in  
`send' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:1253:in`perform\_action\_without\_filters’  
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action\_controller/filters.rb:617:in  
`call_filters' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/filters.rb:610:in`perform\_action\_without\_benchmark’  
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action\_controller/benchmarking.rb:68:in  
`perform_action_without_rescue' C:/Ruby/lib/ruby/1.8/benchmark.rb:293:in`measure’  
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action\_controller/benchmarking.rb:68:in  
`perform_action_without_rescue' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/rescue.rb:136:in`perform\_action\_without\_caching’  
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action\_controller/caching/sql\_cache.rb:13:in  
`perform_action' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in`cache’  
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active\_record/query\_cache.rb:8:in  
`cache' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/caching/sql_cache.rb:12:in`perform\_action’  
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action\_controller/base.rb:524:in  
`send' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:524:in`process\_without\_filters’  
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action\_controller/filters.rb:606:in  
`process_without_session_management_support' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/session_management.rb:134:in`process’  
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action\_controller/base.rb:392:in  
`process' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:183:in`handle\_request’  
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action\_controller/dispatcher.rb:110:in  
`dispatch_unlocked' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:123:in`dispatch’  
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action\_controller/dispatcher.rb:122:in  
`synchronize' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:122:in`dispatch’  
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action\_controller/dispatcher.rb:132:in  
`dispatch_cgi' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:39:in`dispatch’  
C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/…/lib/mongrel/rails.rb:76:in  
`process' C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/../lib/mongrel/rails.rb:74:in`synchronize’  
C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/…/lib/mongrel/rails.rb:74:in  
`process' C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:159:in`process\_client’  
C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:158:in  
`each' C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:158:in`process\_client’  
C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:285:in  
`run' C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:285:in`initialize’  
C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:285:in  
`new' C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:285:in`run’  
C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:268:in  
`initialize' C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:268:in`new’  
C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:268:in  
`run' C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/configurator.rb:282:in`run’  
C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/configurator.rb:281:in  
`each' C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/configurator.rb:281:in`run’  
C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/mongrel\_rails:128:in  
`run' C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/command.rb:212:in`run’  
C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/mongrel\_rails:281  
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active\_support/dependencies.rb:142:in  
`load_without_new_constant_marking' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:142:in`load’  
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active\_support/dependencies.rb:521:in  
`new_constants_in' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:142:in`load’  
C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/commands/servers/mongrel.rb:64  
C:/Ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:27:in  
`gem_original_require' C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in`require’  
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active\_support/dependencies.rb:153:in  
`require' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:521:in`new\_constants\_in’  
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active\_support/dependencies.rb:153:in  
`require' C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/commands/server.rb:49 C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in`gem\_original\_require’  
C:/Ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:27:in  
`require’  
script/server:6

- Farmer

---

<div class="post-metadata">

**Author:** ![farmerss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@farmerss](https://www.ruby-forum.com/u/farmerss)\
**Post date:** [December 10, 2008, 9:36am UTC](https://www.ruby-forum.com/t/mysql-error-inserting-blob/155290/2 "2008-12-10T09:36:22Z")

</div>

Actually, the variable in my my.ini file is max\_allowed\_packet, not  
max\_packet\_size.

---

<div class="post-metadata">

**Author:** ![farmerss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@farmerss](https://www.ruby-forum.com/u/farmerss)\
**Post date:** [December 10, 2008, 9:48am UTC](https://www.ruby-forum.com/t/mysql-error-inserting-blob/155290/3 "2008-12-10T09:48:44Z")

</div>

we have had the same problem and it turned out to be the size of the  
column in mysql

whenever we create a blog column we go through and change the type to  
LONGBLOB using the following in the migration.

execute “ALTER TABLE pgcs CHANGE pgc pgc LONGBLOB;”

Simon

On Wed, 10 Dec 2008 17:04:25 +0900, Farmer Schlutzenberg

---

<div class="post-metadata">

**Author:** ![farmerss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@farmerss](https://www.ruby-forum.com/u/farmerss)\
**Post date:** [December 10, 2008, 10:02am UTC](https://www.ruby-forum.com/t/mysql-error-inserting-blob/155290/4 "2008-12-10T10:02:32Z")

</div>

Hi Simon,  
I have it set on MEDIUMBLOB, which the MySQL documentation says has a  
limit of 2^24 bytes, so well above 820K. It was previously on BLOB, and  
I was getting a different error (something like “data too big for  
column”). I just used :limit =\> size\_limit as a condition in a rails  
migration, and this changed the column type from BLOB to MEDIUMBLOB.  
Thanks tho.

---

<div class="post-metadata">

**Author:** ![farmerss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@farmerss](https://www.ruby-forum.com/u/farmerss)\
**Post date:** [December 10, 2008, 10:13am UTC](https://www.ruby-forum.com/t/mysql-error-inserting-blob/155290/5 "2008-12-10T10:13:58Z")

</div>

I just know that when we had that ‘mysql has gone away’ error, the mysql  
logs had something like ‘value too large for col’ etc…

But if that isn’t your problem, then I have nothing else to offer ☹

On Wed, 10 Dec 2008 18:02:32 +0900, Farmer Schlutzenberg

---

<div class="post-metadata">

**Author:** ![farmerss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@farmerss](https://www.ruby-forum.com/u/farmerss)\
**Post date:** [December 10, 2008, 12:03pm UTC](https://www.ruby-forum.com/t/mysql-error-inserting-blob/155290/6 "2008-12-10T12:03:18Z")

</div>

I’ve now worked it out.  
I eventually decided to restart my computer on the off chance that MySQL  
wasn’t actually reconfiguring after I set max\_allowed\_packet (even  
though it seemed this shouldn’t actually affect things since I was only  
changing from 1M to 32M, and my query seems to be less than 1M). This  
fixed the problem.

It seems odd to me that I should need to restart my computer to get  
MySQL to recognise the change - I did restart my server after the config  
change to 32M, so I expected the connection made to MySQL to use the 32M  
packet size. If anyone knows how to configure MySQL to not require  
computer restarts (this is in Vista), that would be good to know.

Even stranger is that with a supposed limit of 1M, the following code -

puts blobstring.class  
puts blobstring.length  
Record.new(:field\_1 =\> integer\_1, :field\_2 =\> integer\_2, :field\_3 =\>  
word, :field\_4 =\> integer\_4, :blob\_field =\> blobstring).save

outputs:

String  
834880

and causes a “MySQL has gone away” error, on response to the MySQL  
insert command caused by the “save” call above, yet with  
max\_allowed\_packet=32M, it works fine. Maybe there are is more than just  
the query string itself contributing to the 1 megabyte?

- Farmer

- Farmer

---

<div class="post-metadata">

**Author:** ![farmerss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@farmerss](https://www.ruby-forum.com/u/farmerss)\
**Post date:** [December 10, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/mysql-error-inserting-blob/155290/7 "2008-12-10T12:05:13Z")

</div>

And, in field\_3 it was just word=“f”, so that shouldn’t have added too  
much. And, as I mentioned earlier, cutting the blob size down to 800K  
also made it work OK.
