简体   繁体   English

Rails 3 Mysql问题

[英]Rails 3 Mysql Problems

Trying to start a new Rails 3 beta 4 app with mysql.... Running OS X Snow Leopard. 尝试使用mysql启动新的Rails 3 beta 4应用程序....运行OS X Snow Leopard。 WIth previous versions of Rails I have no problem with MySQL. 使用以前版本的Rails,我对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: 但是现在当我启动Rails 3应用程序时,当我在Rails index.html启动屏幕上单击“关于您的应用程序环境”时,我收到以下错误:

undefined method `init' for Mysql:Class mysql的未定义方法`init':Class

Change your Gemfile to use 'mysql2', it's a more modern driver and has nicer features as other people have mentioned. 更改您的Gemfile以使用'mysql2',它是一个更现代的驱动程序,并具有其他人提到的更好的功能。

New Rails applications use the mysql2 gem by default. 新的Rails应用程序默认使用mysql2 gem。

I ran into the same issue (RoR 3, OSX 10.6, mysql 2.8.1 gem). 我遇到了同样的问题(RoR 3,OSX 10.6,mysql 2.8.1 gem)。

You can use irb to rule out RoR: 您可以使用irb来排除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. 如果上述方法不起作用,您可能想尝试删除mysql gem并重新安装它。 I came across a post saying bundle install might mess up the install without displaying errors. 我遇到一个帖子说bundle install可能会破坏安装而不会显示错误。

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. 通过irb验证工作正常,然后再次启动rails。

我最终从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. 对于简单的用法,这是典型的(连接,查询,迭代结果),我发现mysql2 gem比mysql或ruby-mysql gems快得多,并且自动将值转换为正确的类型。 And it installes perfectly on Snow Leopard while I couldn't get mysql gem to work. 并且它完全安装在Snow Leopard上,而我无法让mysql gem工作。

More info at http://github.com/brianmario/mysql2 有关更多信息,请访问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). 我想会发生的是mysql gem无法加载mysql动态库(应该由本机MySQL安装提供)。 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. 如果它无法加载这个低级别的mysql_api (实际上它为mysql gem提供了这些功能),它将为你提供一些可能有用的错误。 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 http://wonko.com/post/how-to-install-the-mysqlruby-gem-on-mac-os-x-leopard ,执行此操作

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 : 然后从http://alexbraunstein.com/2011/08/12/library-loaded-libmysqlclient-18-dylib/放入.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 . 在我的情况下,问题是没有使用捆绑器正确安装mysql gem。 when I did this: 当我这样做时:

bundle install mysql (noobish mistake) 捆绑安装mysql(noobish错误)

all gems went to mysql directory, but later on I have checked the docs of the bundler gem and did this: 所有的宝石都去了mysql目录,但后来我检查了bundler gem的文档,并做了这样的事情:

bundle install bundler_files ( to know where the gems are in the future) bundle install bundler_files(知道未来宝石的位置)

everything looked almost ok except that when mysql gem was installing i got some errors. 一切看起来几乎没有,除了当mysql gem安装时我遇到了一些错误。 I noticed that it was because of my folder path "/home/pawel/Aptana Studio Workspace/myrails_app" 我注意到这是因为我的文件夹路径“/ 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 如果你的文件夹路径中有空格,这个gem将无法正确安装,稍后当你修改一个没有空格的路径并尝试安装mysql gem WOW显示任何错误,但安装将被破坏,因为你会有一些额外的文件夹那里有一些文件等等

DELETE THE GEM FOLDER CREATED BY BUNDLER AND REINSTALL GEMS WITH THIS COMMAND: 删除BUNDLER创建的GEM文件夹并使用此命令重新安装GEMS:

bundle install 捆绑安装

That solved the problem. 这解决了这个问题。

you can try switching to the mysql2 gem which should resolve all that issues for you. 您可以尝试切换到mysql2 gem,它应该为您解决所有问题。 see: https://github.com/brianmario/mysql2/ 请参阅: https//github.com/brianmario/mysql2/

I has the same issue after upgrading to Snow Leopard. 升级到Snow Leopard后,我遇到了同样的问题。 On installing the MySQL gem, I got a bunch of errors about the documentation, then, on running the server: 在安装MySQL gem时,我收到了一些关于文档的错误,然后,在运行服务器时:

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: 我还与其他宝石有一些相似但无关的问题,特别是那些需要编译的C组件,如RedCloth和hpricot:

Unable to compile native extensions

These errors were to do with native extensions in base 64 architecture. 这些错误与base 64架构中的本机扩展有关。 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. 我重新安装了XCode 4.升级到Snow Leopard已经打破了我的C编译器,所以有些宝石无法编译。 This took me a step closer, but didn't fix the issue. 这让我更近了一步,但没有解决问题。
  2. I blew away and reinstalled RVM. 我吹走了并重新安装了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. 这修复了每个本机架构base64错误,但MySQL gem仍然失败。
  3. I removed and downgraded MySQL to version 5.1. 我删除并将MySQL降级到5.1版。 This fixed the MySQL gem issue. 这修复了MySQL gem问题。

All is now well again. 现在一切都好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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