mysql2 和 ruby on rails

作者: EI__Nino
发布时间:2015-07-13 11:15:31

遇到ruby on rails 转到mysql问题,首先使用 gem mysql2 不是 mysql
确认 Gemfile里面加入了 gem mysql2
确认 database.yml 里,使用 adapter: mysql2 
确认安装好 mysql2 , gem install mysql -v 'x.x.x' 或者bundle install 自动安装

如果出现 LoadError   mysql2.bundle  参考如下
(如下文章来自 http://hlee.iteye.com/blog/1202446)
引用

OS Version: Mac OS X 10.7.1 Lion 
Ruby Version: ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.1.0] 
Ruby Location: /usr/local/rvm/bin/ruby 
Rails Version: Rails 3.1.0 
Rails Location: /usr/local/rvm/gems/ruby-1.9.2-p290/bin/rails 


错误提示 
引用

/usr/local/rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.3.7/lib/mysql2.rb:9:in `require': dlopen(/usr/local/rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.3.7/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib (LoadError) 
  Referenced from: /usr/local/rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.3.7/lib/mysql2/mysql2.bundle 
  Reason: image not found - /usr/local/rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.3.7/lib/mysql2/mysql2.bundle 
    from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.3.7/lib/mysql2.rb:9:in `<top (required)>' 
    from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.18/lib/bundler/runtime.rb:68:in `require' 
    from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.18/lib/bundler/runtime.rb:68:in `block (2 levels) in require' 
    from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.18/lib/bundler/runtime.rb:66:in `each' 
    from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.18/lib/bundler/runtime.rb:66:in `block in require' 
    from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.18/lib/bundler/runtime.rb:55:in `each' 
    from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.18/lib/bundler/runtime.rb:55:in `require' 
    from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.18/lib/bundler.rb:120:in `require' 
    from /Users/doug/Sites/simple_cms/config/application.rb:7:in `<top (required)>' 
    from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.0/lib/rails/commands.rb:52:in `require' 
    from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.0/lib/rails/commands.rb:52:in `block in <top (required)>' 
    from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.0/lib/rails/commands.rb:49:in `tap' 
    from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.0/lib/rails/commands.rb:49:in `<top (required)>'
    from script/rails:6:in `require' 
    from script/rails:6:in `<main>' 

解决办法 
Ruby代码  收藏代码
  1. sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /Users/keo/.rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.3.7/lib/mysql2/mysql2.bundle  


还有一个装mongrel的出错 

出错提示如下: 
引用

ding native extensions.  This could take a while... 
ERROR:  Error installing mongrel: 
    ERROR: Failed to build gem native extension. 

/usr/local/bin/ruby extconf.rb install mongrel 
checking for main() in -lc... yes 
creating Makefile 

make 
gcc -I. -I/usr/local/include/ruby-1.9.1/i386-darwin9.7.0 -I/usr/local/include/ruby-1.9.1/ruby/backward -I/usr/local/include/ruby-1.9.1 -I. -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE   -fno-common -D_XOPEN_SOURCE=1 -O2 -g -Wall -Wno-parentheses  -fno-common -pipe -fno-common  -o http11.o -c http11.c 
http11.c: In function 'http_field': 
http11.c:77: error: 'struct RString' has no member named 'ptr' 
http11.c:77: error: 'struct RString' has no member named 'len' 
http11.c:77: warning: left-hand operand of comma expression has no effect 
http11.c:77: warning: statement with no effect 
http11.c: In function 'header_done': 
http11.c:172: error: 'struct RString' has no member named 'ptr' 
http11.c:174: error: 'struct RString' has no member named 'ptr' 
http11.c:176: error: 'struct RString' has no member named 'ptr' 
http11.c:177: error: 'struct RString' has no member named 'len' 
http11.c: In function 'HttpParser_execute': 
http11.c:298: error: 'struct RString' has no member named 'ptr' 
http11.c:299: error: 'struct RString' has no member named 'len' 
make: *** [http11.o] Error 1 

问题是要装pre的mongrel才能1.9.2 rails 3.1 
Ruby代码  收藏代码
  1. $ sudo gem1.9 install mongrel --pre  
  2. > Building native extensions.  This could take a while...  
  3. > Successfully installed mongrel-1.2.0.pre2  
  4. > 1 gem installed  


最好是改gemfile如下: 
Ruby代码  收藏代码
  1. gem 'mongrel','~> 1.2.0.pre2'  



版权声明:本文为博主原创文章,未经博主允许不得转载。

来源:http://blog.csdn.net/EI__Nino/article/details/39779977

推荐: