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
To fix, you can increase the maximal packet size limit max_allowed_packet in my. cnf file, eg. set max_allowed_packet = 128M , then restart your MySQL server: sudo /etc/init. d/mysql restart.
The MySQL server has gone away error, which means that the MySQL server (mysqld) timed out and closed the connection. By default, MySQL will close connections after eight hours (28800 seconds) if nothing happens.
A Connection Timed Out error occurs when the database's firewall won't allow you to connect to the database from your local machine or resource. If you are getting this error, check that you have added the machine or resource you are connecting from to the database's list of trusted sources.
The largest possible packet that can be transmitted to or from a MySQL 8.0 server or client is 1GB. When a MySQL client or the mysqld server receives a packet bigger than max_allowed_packet bytes, it issues an ER_NET_PACKET_TOO_LARGE error and closes the connection.
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:
reconnect: true
to the database specification in database.yml
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With