'undefined init method for mysql: class'

I'm having trouble installing MySQL Server, which got messed up after a power outage.

Configuration

Intel i5 Mac runs on OS X 10.6.5
Installed Ruby 1.9.2
Rails 3.0.1 installed
MySQL Server (finally) installed and running
I completely reinstalled MySQL, which removed the local development / testing / production databases. So, I ran create database development; in MySQL to get the dev database for migration.

Current goal

  • Run rake db:migrate to return my databases again. (Currently, I cannot access my databases or Mysql from Rails.)

Mistake

Using gem 'mysql', '2.8.1' and run rake db:migrate , I get an error:

 rake aborted! undefined method 'init' for Mysql:Class 

Stack trace:

 /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/connection_adapters/mysql_adapter.rb:30:in 'mysql_connection' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:230:in 'new_connection' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:238:in 'checkout_new_connection' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:194:in 'block (2 levels) in checkout' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:190:in 'loop' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:190:in 'block in checkout' /Users/sscirrus/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/monitor.rb:201:in 'mon_synchronize' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:189:in 'checkout' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:96:in 'connection' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:318:in 'retrieve_connection' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract/connection_specification.rb:97:in 'retrieve_connection' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract/connection_specification.rb:89:in 'connection' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/migration.rb:486:in 'initialize' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/migration.rb:433:in 'new' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/migration.rb:433:in 'up' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/migration.rb:415:in 'migrate' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/railties/databases.rake:142:in 'block (2 levels) in <top (required)>' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake.rb:636:in 'call' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake.rb:636:in 'block in execute' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake.rb:631:in 'each' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake.rb:631:in 'execute' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake.rb:597:in 'block in invoke_with_call_chain' /Users/sscirrus/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/monitor.rb:201:in 'mon_synchronize' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake.rb:590:in 'invoke_with_call_chain' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake.rb:583:in 'invoke' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake.rb:2051:in 'invoke_task' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake.rb:2029:in 'block (2 levels) in top_level' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake.rb:2029:in 'each' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake.rb:2029:in 'block in top_level' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake.rb:2068:in 'standard_exception_handling' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake.rb:2023:in 'top_level' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake.rb:2001:in 'block in run' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake.rb:2068:in 'standard_exception_handling' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake.rb:1998:in 'run' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/bin/rake:31:in '<top (required)>' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/bin/rake:19:in 'load' /Users/sscirrus/.rvm/gems/ruby-1.9.2-p0/bin/rake:19:in '<main>' 
+6
mysql ruby-on-rails ruby-on-rails-3 macos
source share
8 answers

What you are looking for is right here .

Oluf Nielsen

+3
source share

I have Rails 3.1-rc4, MySQL 5.5.15 (just updated) and Mac OS X Lion. My problem was that the mysql_api.bundle file was associated with the desired libmysqlclient.18.dylib file, but it could not find the library:

 otool -L ~/.rvm/gems/ruby-1.9.2-p180/gems/mysql-2.8.1/lib/mysql_api.bundle /Users/rowland/.rvm/gems/ruby-1.9.2-p180/gems/mysql-2.8.1/lib/mysql_api.bundle: /Users/rowland/.rvm/rubies/ruby-1.9.2-p180/lib/libruby.1.9.1.dylib (compatibility version 1.9.1, current version 1.9.1) libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.0.0) /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0) 

The library was not found at runtime. My first attempt to fix this was to add the LC_RPATH command, but that wasn’t enough (perhaps because it was at the end of the list). Instead, I directly changed the library link:

 install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib ~/.rvm/gems/ruby-1.9.2-p180\@rails-3.1/gems/mysql-2.8.1/lib/mysql_api.bundle 

Now otool shows me:

 otool -L ~/.rvm/gems/ruby-1.9.2-p180\@rails-3.1/gems/mysql-2.8.1/lib/mysql_api.bundle /Users/rowland/.rvm/gems/ ruby-1.9.2-p180@rails-3.1 /gems/mysql-2.8.1/lib/mysql_api.bundle: /Users/rowland/.rvm/rubies/ruby-1.9.2-p180/lib/libruby.1.9.1.dylib (compatibility version 1.9.1, current version 1.9.1) /usr/local/mysql/lib/libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.0.0) /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0) 

and he is happy again. This only works on Mac OS X.

+4
source share

This line helped me get rid of the error above. On Mac OS X 10.7. a lion

 export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:$DYLD_LIBRARY_PATH" >> ~/.profile 
+2
source share

no other solution worked for me, but:

 brew install mysql-connector-c sudo gem install mysql -- --with-mysql-lib=/usr/local/mysql/lib 

ruby mysql with native extensions on mac osx

+1
source share

I came across the same error while trying to get a rails site running on CentOS using the mysql-2.8.1 gem. I could start the site just fine if I started it as root, but when I switched to my application user, it would throw this error. In the end, these were permissions on the mysql directory structure. I ran chmod -R 755 in the mysql directory and restarted my rails server and the error went away. The error is a bit vague and seems to cover everything when the driver connects to the database.

+1
source share

This worked for me on ubuntu

 export LD_LIBRARY_PATH=/usr/local/mysql/lib ldconfig 

I just put this at the bottom of /etc/profile and everything works fine for any new logins. You can quickly test it without logging in by doing this

 source /etc/profile 
0
source share

Adding to Shaun's answer, which I was able to use:

My setup is OS X Lion + apache + passenger + mysql installed via Homebrew. When I start the "rails server", I should have set the variable DYLD_LIBRARY_PATH, but I could not figure out how to set it in apache. I had to do this:

 cd /Library/Ruby/Gems/1.8/gems/mysql-2.8.1/lib sudo install_name_tool -change libmysqlclient.18.dylib `brew --prefix mysql`/lib/libmysqlclient_r.18.dylib mysql_api.bundle 

The same answer, just specific to the Homebrew installation, and my gems were not in ~ / .rvm / gems like him.

0
source share

I am getting the same error, but I am replacing the gem from 'mysql' to 'mysql2', and its working for me. So I shared people with you if anyone else has the same error.

0
source share

All Articles