Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'undefined method init for Mysql:Class'

I've been having problems with a MySQL Server installation that got messed up after a power outage.

Configuration

Intel i5 Mac running OS X 10.6.5
Ruby 1.9.2 installed
Rails 3.0.1 installed
MySQL Server (finally) installed and running
I completely reinstalled MySQL, which deleted the local development/test/production databases. So, I have run create database development; in MySQL to get the dev database ready for a migration.

Current Goal

  • Run rake db:migrate to get my databases back again. (I cannot currently access my databases or Mysql at all from Rails.)

Error

Using the gem 'mysql', '2.8.1' and run rake db:migrate, I get the 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>'
like image 530
sscirrus Avatar asked Dec 28 '10 11:12

sscirrus


5 Answers

I have Rails 3.1-rc4, MySQL 5.5.15 (just upgraded), and Mac OS X Lion. My issue turned out to be that the mysql_api.bundle file was linked to the right 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)

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

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/[email protected]/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 it is all happy again. This only works on Mac OS X though.

like image 77
Shaun Rowland Avatar answered Oct 21 '22 21:10

Shaun Rowland


What you're looking is right here.

Oluf Nielsen

like image 27
Oluf Nielsen Avatar answered Oct 21 '22 21:10

Oluf Nielsen


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

export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:$DYLD_LIBRARY_PATH" >> ~/.profile
like image 42
vladimeeer Avatar answered Oct 21 '22 21:10

vladimeeer


I ran into this same error trying to get a rails site working on CentOS using the mysql-2.8.1 gem. I could run the site just fine if I started it as root, but when I switched to my application user it would throw this error. It ended up being the permissions on the mysql directory structure. I ran a chmod -R 755 on the mysql directory and restarted my rails server and the error went away. The error is a little vague and seems to cover everything when the driver is connecting to the database.

like image 1
Dan Avatar answered Oct 21 '22 20:10

Dan


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

like image 1
daniel Avatar answered Oct 21 '22 19:10

daniel