简体   繁体   中英

Rails 3 Mysql Problems

Trying to start a new Rails 3 beta 4 app with mysql.... Running OS X Snow Leopard. WIth previous versions of Rails I have no problem with MySQL. But now when I start the Rails 3 app I get the following error when I click "About Your Application Environment" on the Rails index.html startup screen:

undefined method `init' for Mysql:Class

Change your Gemfile to use 'mysql2', it's a more modern driver and has nicer features as other people have mentioned.

New Rails applications use the mysql2 gem by default.

I ran into the same issue (RoR 3, OSX 10.6, mysql 2.8.1 gem).

You can use irb to rule out RoR:

irb
require 'rubygems'
require 'mysql'
db = Mysql.connect('hostname', 'username', 'password', 'database')

If the above doesn't work, you may want to try removing the mysql gem and reinstalling it. I came across a post saying bundle install might mess up the install without displaying errors.

sudo gem uninstall mysql
sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

Verify things work via irb , then start up rails again.

我最终从mysql gem切换到ruby-mysql gem,工作。

For simple usage, which is typical (connecting, querying, iterating over results), I found mysql2 gem which is much faster than mysql or ruby-mysql gems and auto-casts values to proper types. And it installes perfectly on Snow Leopard while I couldn't get mysql gem to work.

More info at http://github.com/brianmario/mysql2

I think what happens is that the mysql gem isn't able to load the mysql dynamic library (supposed to be supplied by the native MySQL installation). To test whether this is happening, do this

$ irb
1.9.2p320 :001 > require 'mysql_api'
 => true 
1.9.2p320 :002 > 

If it isn't able to load this low level mysql_api , (which actually supplies the functionality to the mysql gem), it will give you some potentially useful errors. Usually it is not able to find the dynamic library. To remedy this, I found a couple of solutions:

From http://wonko.com/post/how-to-install-the-mysqlruby-gem-on-mac-os-x-leopard , do this

For system-wide install

sudo env ARCHFLAGS="-arch i386" gem install mysql -- \
  --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib \
  --with-mysql-include=/usr/local/mysql/include

or local install

env ARCHFLAGS="-arch i386" gem install mysql -- \
  --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib \
  --with-mysql-include=/usr/local/mysql/include

and then from http://alexbraunstein.com/2011/08/12/library-loaded-libmysqlclient-18-dylib/ put in .bash_profile :

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH 

I think I have found the solution for the problem. In my case the problem was that the mysql gem hasn't been properly installed using the bundler . when I did this:

bundle install mysql (noobish mistake)

all gems went to mysql directory, but later on I have checked the docs of the bundler gem and did this:

bundle install bundler_files ( to know where the gems are in the future)

everything looked almost ok except that when mysql gem was installing i got some errors. I noticed that it was because of my folder path "/home/pawel/Aptana Studio Workspace/myrails_app"

If you have spaces in your folder path this gem wont install properly and later on when you modify the path to one without spaces and try to install the mysql gem IT WONT DISPLAY ANY ERRORS, but the installation will be corrupted, because you will have some extra folders there with some files etc. so

DELETE THE GEM FOLDER CREATED BY BUNDLER AND REINSTALL GEMS WITH THIS COMMAND:

bundle install

That solved the problem.

you can try switching to the mysql2 gem which should resolve all that issues for you. see: https://github.com/brianmario/mysql2/

I has the same issue after upgrading to Snow Leopard. On installing the MySQL gem, I got a bunch of errors about the documentation, then, on running the server:

undefined method `init' for Mysql:Class

I was also having some similar, but unrelated issues with other gems, particularly those that had C components that needed compiling such as RedCloth and hpricot:

Unable to compile native extensions

These errors were to do with native extensions in base 64 architecture. The solution was threefold:

  1. I reinstalled XCode 4. The upgrade to Snow Leopard had broken my C compiler, so some gems were failing to compile. This took me a step closer, but didn't fix the issue.
  2. I blew away and reinstalled RVM. It appeared to be installing gems in one directory, and finding them in another. This fixed every native architecture base64 error, but the MySQL gem was still failing.
  3. I removed and downgraded MySQL to version 5.1. This fixed the MySQL gem issue.

All is now well again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM