Ruby on Rails
Mysql Connection Problems

MySQL Connection Problems

Often connection problem with the database are due to the Ruby MySQL bindings. For performance considerations it’s better to have the mysql-ruby C bindings installed.

Rake db:migrate fails.

This happened to me when I followed the installation instructions at
http://maconrails.com (I’ve got OS X 10.4.10). The error looked like:

dyld: NSLinkModule() error
dyld: Library not loaded: /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib Referenced from: /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle Reason: image not found

At the end of the article http://hivelogic.com/articles/ruby-rails-mongrel-mysql-osx/ there is a comment about needing to change names:

“There’s an issue with the current version of MySQL and the latest Ruby. Fortunately, this is easily fixed with the following command in Terminal:

sudo install_name_tool -change /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib /usr/local/mysql/lib/libmysqlclient.15.dylib /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle

I did this and was able to rake.

“lost connection to mysql server during query”

This is a known problem on Ubuntu. To get rid of this message install libmysql-ruby.

  apt-get install libmysql-ruby1.8

I had the same problem on Debian, “lost connection…” and this fixed it.

Gentoo users:

emerge mysql-ruby 

On Fedora:

 yum install ruby-mysql.i386

Worked for me. Gem install did not.

make sure that mysql-devel is installed and that you have run ldconfig.

    gem install mysql

or (If you install MySQL binary to /usr/local/mysql)
    gem install mysql -- --with-mysql-dir=/usr/local/mysql

I found the following worked for me (Fedora core 3):

gem install mysql -- --with-mysql-include=/usr/include/mysql --with-mysql-lib=/usr/lib/mysql

It seems most simplified with gem (It works on Fedora Core 4):

gem install mysql -- --with-mysql-config=`which
mysql_config`


FC4 user here yes! this works after googling for hours the part that fixed it was using `which mysql_config` instead of the hard path ”/usr/lib/mysql/mysql_config” on my FC4 box `which mysql_config` resolves to ”/usr/bin/mysql_config”

On CentOS 4.5:

Install any supporting packages (note CentOS does not include a “ruby-mysql” package.)

 yum install mysql-devel ruby-devel gcc

Install the gem. Choose the latest ruby version:
gem install mysql -- --with-mysql-include=/usr/include/mysql --with-mysql-lib=/usr/lib/mysql

On Windows:
gem install mysql

I selected the first of the following options it then gave me:
  1. mysql 2.7.2006.04.21 (mswin32)
  2. mysql 2.7 (ruby)
  3. mysql 2.6 (ruby)
  4. mysql 2.5.1 (ruby)
  5. Cancel installation

If this doesn’t install, look here (this solved it on RHEL4)
Also, it’s generally advisable to insure you have mysql-devel installed before attempting to build your own mysql C bindings.

Problems on Windows still?
An alternative MySQL/Ruby distribution for Windows is at http://www.vandomburg.net/pages/mysql-ruby-windows that seems to solve all the corner-cases.

Something else to try
If your code fails after running at least one query successfully, and you are running on WEBrick, apply this patch to your code: http://dev.rubyonrails.org/ticket/3433

It May be as Simple as Passwords
Really, check that any passwords for user(s), database(s), connection(s), and database.yml match. I happen to be using a GUI to interact with my MYSQL databases and by simply editing my users’ and the database connection’s passwords I was able to get rid of the “lost connection” and “access denied for user…” errors.

Flush Privileges

I solved this problem by simply running FLUSH PRIVILEGES after my GRANT statement(s).

This is another solution
I found a completely different solution at HowTosWorkerThreads

Using Solaris 10?

I tried doing pretty much everything mentioned above (installed the gem, password modifications, etc)...along with numerous suggestions from sites I could find via Google. In the end, I installed MySQL/Ruby 2.7.1 from the source and it worked like a charm. I am running

Heed the default database.yml
“And be sure to use new-style password hashing:”
http://dev.mysql.com/doc/refman/5.0/en/old-client.html

I used the mysql_fix_privilege_tables script to upgrade the mysql.user table on my installation. Note that the server must be restarted for mysql to notice the change in size of the password field.

Then, change your mysql password so it gets the new style hashing. New style passwords begin with a ’*’.

See the man page for all the details:

man mysql_fix_privilege_tables

Once I upgraded to the new style hashing, the error disappeared.

Debian

The gem installation lacks symlinking the shared object file where require method looks for it, /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7
so just symlink mysql.so → lib/mysql.so under the same directory.