Standalone script reports "MySQL server has gone away"; problem with my.cnf?
I have a ruby script that requires 'mysql2' and uses 开发者_C百科query statements to mysql like
@db.query("SELECT sname,id FROM streets where region_id=#{region["id"]}")
but it reports me a error always like this:
main.rb:261:in `query': MySQL server has gone away (Mysql2::Error)
from main.rb:261:in `block (3 levels) in <main>'
from main.rb:258:in `each'
from main.rb:258:in `block (2 levels) in <main>'
from main.rb:254:in `each'
from main.rb:254:in `block in <main>'
from main.rb:253:in `loop'
from main.rb:253:in `<main>'
The script on my Ubuntu is OK, however, my production environment is CentOS and on there I installed MySQL from source code. My Rails app run there is OK and it also requires 'mysql2', but when I run this script it fails.
I think the problem is related to my.cnf but I don't know how configure it.
The mysql2 gem provides an option to reconnect to MySQL in the event that the connection goes away. Rather than updating your my.cnf file, update your gem's database.yml file to include the
reconnect: true
option. This will ensure that the mysql2 gem will attempt to reconnect to MySQL when required.
you call .query method directly on object @db... what exactly is this object?
I mean... if you are under Rails, you should call queries like:
result = ActiveRecord::Base.connection.execute("sql here")
I think it could be the cause, because Rails manages connection pool (eg. 5 established connections and every query goes to one of them, based on requests).
Anyway, if this is not the cause, then it can be that your mysql2 gem is compiled with different version of libmysqlclient.so library on the system... Then when you try to do a query, it internally throws an error about incompatible protocol and disconnects...
Please check that your libmysqlclient.so and it's header files are of the same version... Or if you don't have more versions of them installed... and then please try to recompile mysql2 gem.
Hope this helps. Regards, NoICE
精彩评论