# Find\_by\_name -\> Lost connection to MYSQL server during query

**URL:** <https://www.ruby-forum.com/t/find-by-name-lost-connection-to-mysql-server-during-query/81932>\
**Category:** Rails\
**Created:** [January 16, 2007, 12:18pm UTC](https://www.ruby-forum.com/t/find-by-name-lost-connection-to-mysql-server-during-query/81932 "2007-01-16T12:18:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 16, 2007, 12:18pm UTC](https://www.ruby-forum.com/t/find-by-name-lost-connection-to-mysql-server-during-query/81932/1 "2007-01-16T12:18:31Z")

</div>

```
* [Instant Rails Manager] 1.4

* [Ruby 1.8.5 (from the [HTTP://rubyinstaller.rubyforge.org/

```

One-Click Ruby Installer] 1.8.5-21)

```
* [Ruby on Rails] 1.1.6

* [Apache] 1.3.33

* [MySQL] 4.1.9

* [MySQL/Ruby] 2.7 (native driver)

* [Mongrel] 0.3.13.3

* [phpMyAdmin] 2.6.1

```

Above is my setting.  
I have no problem using rails with MYSQL until I’m using the  
find\_by\_columnname  
method.

This is the error message when I tested in the console.  
ActiveRecord::StatementInvalid: Mysql::Error: Lost connection to MySQL  
server du  
ring query: SELECT \* FROM lists WHERE (lists.`user_id` = 1 ) LIMIT 1  
from  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib  
/active\_record/connection\_adapters/abstract\_adapter.rb:120:in `log' from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib /active_record/connection_adapters/mysql_adapter.rb:184:in`execute’  
from  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib  
/active\_record/connection\_adapters/mysql\_adapter.rb:336:in `select' from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib /active_record/connection_adapters/mysql_adapter.rb:175:in`select\_all’  
from  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib  
/active\_record/base.rb:390:in `find_by_sql' from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib /active_record/base.rb:924:in`find\_every’  
from  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib  
/active\_record/base.rb:918:in `find_initial' from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib /active_record/base.rb:1103:in`send’  
from  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib  
/active\_record/base.rb:1103:in `method\_missing’  
from (irb):26

Anyone knows how to fix it?  
Thanks

---

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 16, 2007, 6:52pm UTC](https://www.ruby-forum.com/t/find-by-name-lost-connection-to-mysql-server-during-query/81932/2 "2007-01-16T18:52:25Z")

</div>

How does “SELECT \* FROM lists WHERE (lists.`user_id` = 1 ) LIMIT 1”  
work when you run it in a MySQL environment with the same login  
information that rails is using?

Most of the time when I get that error, it’s a permission problem.  
Otherwise, I’ve had problems when I’ve used a MySQL reserved word by  
mistake.

Just from glancing at the SQL, it looks fine.  
And I don’t see any reserved words from:

[http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html](http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html)

On a related note, in the newer MySQL ruby driver’s there’s a Reconnect  
command, that automatically reconnects to the database if you lose your  
connection.

I don’t think it’s been intergrated with rails yet, and I’m not sure how  
you would turn it on though.

---

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 17, 2007, 2:00am UTC](https://www.ruby-forum.com/t/find-by-name-lost-connection-to-mysql-server-during-query/81932/3 "2007-01-17T02:00:07Z")

</div>

David H. wrote:

> How does “SELECT \* FROM lists WHERE (lists.`user_id` = 1 ) LIMIT 1”  
> work when you run it in a MySQL environment with the same login  
> information that rails is using?

SELECT \* FROM lists WHERE (lists.`user_id` = 1 ) LIMIT 1 in mysql no  
response.  
But in phpmyadmin, it works. In both environment they use root without  
password.

> Most of the time when I get that error, it’s a permission problem.  
> Otherwise, I’ve had problems when I’ve used a MySQL reserved word by  
> mistake.
> 
> Just from glancing at the SQL, it looks fine.  
> And I don’t see any reserved words from:
> 
> [http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html](http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html)
> 
> On a related note, in the newer MySQL ruby driver’s there’s a Reconnect  
> command, that automatically reconnects to the database if you lose your  
> connection.

The newer version Ruby/MySQL is 1.8?  
But I can still do find , just the find\_by\_xxx is not working. I suppose  
I still have connection to MySQL though.

> I don’t think it’s been intergrated with rails yet, and I’m not sure how  
> you would turn it on though.

Thanks for sharing your experience.

---

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 17, 2007, 5:07pm UTC](https://www.ruby-forum.com/t/find-by-name-lost-connection-to-mysql-server-during-query/81932/4 "2007-01-17T17:07:03Z")

</div>

> Bontina C. wrote:  
> I found it’s the syntax the driver generated is wrong.  
> It should be “SELECT \* FROM lists WHERE (lists.user\_id = 1 ) LIMIT 1”  
> without the single quotation. Do you know where can I modify the syntax  
> to the right version?

Very strange, the single quotes should reduce problems, not cause them.

Sounds like a naming conflict, I’d suggest changing the name of the  
table.

Short of that try,

Myobject.find\_by\_sql(“my custom sql”)  
or

List.find\_by\_sql(“SELECT \* FROM lists WHERE (lists.user\_id = 1 ) LIMIT  
1”)

---

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 17, 2007, 2:38am UTC](https://www.ruby-forum.com/t/find-by-name-lost-connection-to-mysql-server-during-query/81932/5 "2007-01-17T02:38:00Z")

</div>

David H. wrote:

> How does “SELECT \* FROM lists WHERE (lists.`user_id` = 1 ) LIMIT 1”  
> work when you run it in a MySQL environment with the same login  
> information that rails is using?
> 
> Most of the time when I get that error, it’s a permission problem.  
> Otherwise, I’ve had problems when I’ve used a MySQL reserved word by  
> mistake.
> 
> Just from glancing at the SQL, it looks fine.  
> And I don’t see any reserved words from:
> 
> [http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html](http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html)
> 
> On a related note, in the newer MySQL ruby driver’s there’s a Reconnect  
> command, that automatically reconnects to the database if you lose your  
> connection.
> 
> I don’t think it’s been intergrated with rails yet, and I’m not sure how  
> you would turn it on though.

I found it’s the syntax the driver generated is wrong.  
It should be “SELECT \* FROM lists WHERE (lists.user\_id = 1 ) LIMIT 1”  
without the single quotation. Do you know where can I modify the syntax  
to the right version?
