Rails Rails 使用 mysql2 出现问题

torubylist · 2015年09月23日 · 最后由 u1445594398 回复于 2015年10月23日 · 6438 次阅读

大家好,我是 rails 新手。我安装的 rails 默认数据库是 sqlite3.但是我想用 mysql。于是百度了一下。 先安装了 mysql 的相关工具,server,client.

netstat -tap |grep mysql
tcp        0      0 localhost:mysql         *:*                     LISTEN      11990/mysqld

rails new app  -d mysql
 rails generate controller Site index about help
错误:Gem::LoadError: Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
/root/railsspace/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/connection_specification.rb:177:in `rescue in spec'

我的 Gemfile:

 1 source 'https://rubygems.org'
 4 # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
 5 gem 'rails', '4.2.3'
 6 # Use mysql as the database for Active Record
 7 gem 'mysql2', '~> 0.4.1'
 9 # Use SCSS for stylesheets
10 gem 'sass-rails', '~> 5.0'
11 # Use Uglifier as compressor for JavaScript assets
12 gem 'uglifier', '>= 1.3.0'
13 # Use CoffeeScript for .coffee assets and views
14 gem 'coffee-rails', '~> 4.1.0'

根据搜的各类帖子,貌似都没有解决掉问题。请问问题出现在哪里?

7 gem 'mysql2', '~> 0.3.0' 0.4 据说是有兼容版本问题。

这个我碰到过,你的 Rails 是 4.2.x 的吧,你在你的 Gemfile 里面把 gem 'mysql2', '~>0.3.6' ,然后 bundle 一下,就可以解决。

#1 楼 @chenge 是的,好像是兼容问题。

#2 楼 @hewe 是的。可是改了之后问题还是没有解决。You have added to the Gemfile:

@torubylist

执行过 bundle 命令了么?

#6 楼 @zhengbotao 执行过 bundle update.

#5 楼 @torubylist 不行是什么不行,把日志贴上来,主动提供线索,不要让帮助你的人猜。

#9 楼 @rei

/root/railsspace/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/connection_specification.rb:177:in `rescue in spec': Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). (Gem::LoadError)
        from /root/railsspace/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec'
        from /root/railsspace/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_handling.rb:50:in `establish_connection'
        from /root/railsspace/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/railtie.rb:120:in `block (2 levels) in <class:Railtie>'
        from /root/railsspace/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
        from /root/railsspace/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
        from /root/railsspace/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
        from /root/railsspace/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/lazy_load_hooks.rb:44:in `each'
        from /root/railsspace/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
        from /root/railsspace/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/base.rb:315:in `<module:ActiveRecord>'
        from /root/railsspace/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/base.rb:26:in `<top (required)>'
        from /usr/local/rvm/gems/ruby-2.2.2/gems/spring-1.4.0/lib/spring/application.rb:319:in `active_record_configured?'
        from /usr/local/rvm/gems/ruby-2.2.2/gems/spring-1.4.0/lib/spring/application.rb:251:in `disconnect_database'
        from /usr/local/rvm/gems/ruby-2.2.2/gems/spring-1.4.0/lib/spring/application.rb:97:in `preload'
        from /usr/local/rvm/gems/ruby-2.2.2/gems/spring-1.4.0/lib/spring/application.rb:143:in `serve'
        from /usr/local/rvm/gems/ruby-2.2.2/gems/spring-1.4.0/lib/spring/application.rb:131:in `block in run'
        from /usr/local/rvm/gems/ruby-2.2.2/gems/spring-1.4.0/lib/spring/application.rb:125:in `loop'
        from /usr/local/rvm/gems/ruby-2.2.2/gems/spring-1.4.0/lib/spring/application.rb:125:in `run'
        from /usr/local/rvm/gems/ruby-2.2.2/gems/spring-1.4.0/lib/spring/application/boot.rb:18:in `<top (required)>'
        from /usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from /usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from -e:1:in `<main>'

Gemfile Gemfile.lock 的内容贴上来。

已解决,谢谢大家。bundle install --no-deployment

#4 楼 @torubylist 好吧,我用这种方法解决了。。。

在 Gemfile 中加上 gem "activerecord-jdbcmysql-adapter", :platforms => :jruby
bundle install

试试

同事建議我試試 gem 'mysql2', '~> 0.3.20'

需要 登录 后方可回复, 如果你还没有账号请 注册新账号