可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
回答1:
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.
回答2:
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.
回答3:
I ended up switching from the mysql gem to the ruby-mysql gem, worked.
回答4:
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
回答5:
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.
回答6:
you can try switching to the mysql2 gem which should resolve all that issues for you. see: https://github.com/brianmario/mysql2/
回答7:
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
回答8:
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:
- 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.
- 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.
- I removed and downgraded MySQL to version 5.1. This fixed the MySQL gem issue.
All is now well again.