Sporadically getting error Mysql::Error: Lost connection

I just setup Rails on Centos5. I have a remote MySQL 4.1 server.

I am able to successfully connect to MySQL with rails and it works fine
most of the time.

However, I get the following error every once in a while (not always on
the same query):

Mysql::Error: Lost connection to MySQL server during query: SHOW FIELDS
FROM groups_rights

Any idea what might be causing this? There doesn’t appear to be any
packet loss between the 2 servers.

Do you have the mysql-ruby bindings installed? I don’t know how you
installed Rails and / or Ruby on your system. Some distros have this
as a
package.

You could also install it with gems

sudo gem install mysql

provided that you have everything set up right on your box (paths,
compilers, etc),

I don’t know Centos at all, so I won’t be much more help than that.
However,
I’ve seen that error before whenever I had heavy dtabase access in my
Rails
apps and they were using the Mysql libraries that comes with Rails
rather
than the much faster C bindings I talked about here.

Hope that helps you some.

On Jan 8, 2008 7:31 AM, Homer S. [email protected]

Thanks for the reply

I installed mysql via:

gem install mysql --with-mysql-config=/usr/bin/mysql_config

I figure if there was a problem with this then I would not be able to
connect at all, right?

Here is what I get - seems about right, correct?

gem list mysql

*** LOCAL GEMS ***

mysql (2.7)
MySQL/Ruby provides the same functions for Ruby programs that the
MySQL C API provides for C programs.

I just enabled MySql logging and I see the query (an update query in
this case) being executed and the rolled back.

Not necessarily. Rails could still be using the native one.

If you do
gem list mysql

does it show up on the list? If it does then I have no idea what’s
wrong. If
it doesn’t show up then it didn’t install (that’s happened to me as
well)

On Jan 8, 2008 8:03 AM, Homer S. [email protected]

On 8 Jan 2008, at 13:31, Homer S. wrote:

Mysql::Error: Lost connection to MySQL server during query: SHOW
FIELDS
FROM groups_rights

Does this happen after long periods of inactivity? Have you tried
setting config.active_record.verification_timeout
You need to set it to something lower than whatever your mysql
connection timeout setting is.

Fred

Does this happen after long periods of inactivity?

No, actually, it seems to happen a lot shortly after I restart the
mongrel process. It doesn’t usually happen on the first hit after the
restart so I know its making the initial connection OK.

For the fun of it I installed MySQL 5 on the same server that rails is
on and I can duplicate this problem. This is feeling like a problem
with the mysql-ruby stuff, doesn’t it?

Any suggestion on how to proceed on troubleshooting?

On 8 Jan 2008, at 16:59, Homer S. wrote:

Is there anyway to verify that the mysql gem is actually being used?

put
raise ‘Bananarama’
somewhere appropriate in the mysql/ruby driver. if you see bananarama
then you’ll know that you’re not running the gem

Fred.

Thanks Fred - I’ll try that out a little later (have a meeting in a min)

  • but one more clue:

This is only happening when using the active_record_store to store
sessions. I canno duplicate when writing session info to file.

I can see in the SQL log that “sometimes” when it does the query to
update the session it creates a new connection to the db (as opposesed
to using the connection that rails process has already created - that is
when the error occurs.

Most of the time the session query is using the same connection as the
rest of the queries.

Thought I’d post that bit of information in the event it rings a bell
for anyone.

Is there anyway to verify that the mysql gem is actually being used?

I uninstalled the mysql gem and I see the same problem.

I tried installing like this:
gem install mysql – --with-mysql-include=/usr/include/mysql
–with-mysql-lib=/usr/lib/mysql

(the above was taken from
http://wiki.rubyonrails.org/rails/pages/Mysql+Connection+Problems -
CentOS 4.5 section)

Its starting to feel like the gem is not even being used. I have
another environment that is on RHEL3 and I don’t have these issues
there.

Maybe there’s some clues in your mysql error log? There may be
something wrong with the table. I’ve gotten similar behavior to this
once when I redefined the size of the innodb data files.

Mysql::Error: Lost connection to MySQL server during query: SHOW FIELDS
FROM groups_rights

there’s a gem to workaround this problem:
google ‘mysql_retry_lost_connection’.

hiro

Raymond O’Connor wrote:

Maybe there’s some clues in your mysql error log? There may be
something wrong with the table. I’ve gotten similar behavior to this
once when I redefined the size of the innodb data files.

No, nothing in the error log

It could be something else that I was experiencing also. Does your
program make concurrent threads?
If it does, make sure you create a semaphore because there is a know
bug in webrick that leads to Mysql Lost connection.
http://wiki.rubyonrails.org/rails/pages/HowTosWorkerThreads

On Jan 9, 2008 1:25 AM, Hiroki Y. [email protected] wrote:

Javier Q. Fernández
[email protected]
Spain +34 618 420 028
Netherlands +31 619 347 865
http://senc.yoteinvoco.com

Anyone have opinions on what payment processors are best? I am using
Payflow right now wondering if I should change or keep using it. Plan
on using ActiveMerchant so want something that it supports.

Any opinions or recommendations out there?

thanks.

I put a debug in the mysql_adapter and indeed the ruby version of mysql
is being used.

I just need to figure out why - it has something to do with the
RubyWorks Production Stack installation. I did not use this on my RHEL
3 machine - so, that explains why its working OK there.

Thanks for all your help so far!

Actually, you can just check the development.log to see if you are
using the correct bindings. If not you’ll get this:

WARNING: You’re using the Ruby-based MySQL library that ships with
Rails. This library is not suited for production. Please install the C-
based MySQL library instead (gem install mysql).

Interestingly I have done ‘gem install mysql’ and I still get this
warning.

Phil

On Jan 8, 5:36 pm, Frederick C. [email protected]

Actually, you can just check the development.log to see if you are
using the correct bindings. If not you’ll get this:

WARNING: You’re using the Ruby-based MySQL library that ships with
Rails. This library is not suited for production. Please install the
C-
based MySQL library instead (gem install mysql).

Interestingly I have done ‘gem install mysql’ and I still get this
warning.

Phil

On Jan 8, 5:36 pm, Frederick C. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs