14

I am getting this error occasionally. I have read some solutions in stackoverflow but they were about rails 2 or mysql. Any help will be appreciated.

ActiveRecord::StatementInvalid (Mysql2::Error: MySQL server has gone away
ziCk
  • 141
  • 1
  • 1
  • 3
  • Does message "MySQL server has gone away" tells you anything? Have you tried to connect to your MySQL server in other when having this error (phpMyAdmnin, console)? – Hnatt Jul 24 '11 at 13:35
  • When i refresh the page it works. – ziCk Jul 24 '11 at 13:39
  • Does this error occur on random pages or in one definite place? Maybe there's a problem with some ineffective query? – Hnatt Jul 24 '11 at 14:27
  • Are you on shared hosting or a private server? – Hnatt Jul 24 '11 at 14:33
  • application is in us and database server is in europe – ziCk Jul 24 '11 at 14:39
  • So I suggest you should ask your european friends if they having troubles with their mysql server. Tell us if that solved your problem. – Hnatt Jul 24 '11 at 14:43
  • ActiveRecord::Base.verify_active_connections! solved it. I created a method and call it as a before filter method – ziCk Jul 24 '11 at 14:47

4 Answers4

8

There are numerous causes for the error. See below page for possible causes. Perhaps your packet size is set too small.

http://dev.mysql.com/doc/refman/5.0/en/gone-away.html

Ray Fix
  • 5,575
  • 3
  • 28
  • 30
5

I got this error while trying to import a large file through seeds.rb with rake db:seed by calling one statement:

ActiveRecord::Base.connection.execute(IO.read("path/to/file.sql"))

And I kept on getting ActiveRecord::StatementInvalid (Mysql2::Error: MySQL server has gone away...


SOLUTION

I resolved this by a combination of two things:

  1. Add reconnect: true to the database specification in database.yml
  2. Read the SQL file and execute the statement individually, as such:

    f = File.new('path/to/file.sql')
    
    while statements = f.gets("") do
      ActiveRecord::Base.connection.execute(statements)
    end  
    

I had to modify to remove some comments from my SQL file -- they made ActiveRecord throw errors for some reason, but that resolved my problem.

chadoh
  • 4,343
  • 6
  • 39
  • 64
Yuval Karmi
  • 26,277
  • 39
  • 124
  • 175
  • Oh, and to give credit where due, I adapted the solution code (step 2) from this blog post: http://nathan.seedoftruth.net/post/15237050763/rails-3-load-sql-directly – Yuval Karmi Dec 23 '13 at 03:20
1

I experience exactly same issue when I run "rake db:reset" command on my development environment. But I never see this error message when I run "rake db:migrate:reset && rake db:seed".

Though it is very strange, but this may throw some lights on this issue. I am glad if my post leads to a solution somehow.

Tsutomu
  • 4,848
  • 1
  • 46
  • 68
  • 2
    I have the same issue running `rake db:reset`. Putting a `ActiveRecord::Base.connection.reconnect!` just before the source line of the exception fixed it. – unnu Nov 23 '12 at 10:17
0

Maybe the server you are hosted on is overloaded and in some cases the MySQL server can not execute a query. Ask your hosting provider about performance monitoring tools, or tell him about this problem directly. This error message should be enough for them to give you an answer.

Hnatt
  • 5,767
  • 3
  • 32
  • 43