我安装了mysql gem但仍显示此错误

时间:2015-01-06 14:31:14

标签: ruby-on-rails gem

D:\Projects\Ruby on Rails\Library>rails generate model Book
      invoke  active_record
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connect
ion_adapters/connection_specification.rb:177:in `rescue in spec': Specified 'mys
ql' for database adapter, but the gem is not loaded. Add `gem 'mysql'` to your G
emfile (and ensure its version is at the minimum required by ActiveRecord). (Gem
::LoadError)
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-4.2.0/lib/active_r
ecord/connection_adapters/connection_specification.rb:174:in `spec'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-4.2.0/lib/active_r
ecord/connection_handling.rb:50:in `establish_connection'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-4.2.0/lib/active_r
ecord/railtie.rb:120:in `block (2 levels) in <class:Railtie>'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.2.0/lib/active_
support/lazy_load_hooks.rb:38:in `instance_eval'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.2.0/lib/active_
support/lazy_load_hooks.rb:38:in `execute_hook'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.2.0/lib/active_
support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.2.0/lib/active_
support/lazy_load_hooks.rb:44:in `each'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.2.0/lib/active_
support/lazy_load_hooks.rb:44:in `run_load_hooks'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-4.2.0/lib/active_r
ecord/base.rb:316:in `<module:ActiveRecord>'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-4.2.0/lib/active_r
ecord/base.rb:26:in `<top (required)>'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.2.0/lib/rails/genera
tors/named_base.rb:179:in `pluralize_table_names?'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.2.0/lib/rails/genera
tors/named_base.rb:119:in `table_name'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-4.2.0/lib/rails/ge
nerators/active_record/model/model_generator.rb:21:in `create_migration_file'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/command.rb
:27:in `run'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/invocation
.rb:126:in `invoke_command'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/invocation
.rb:133:in `block in invoke_all'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/invocation
.rb:133:in `each'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/invocation
.rb:133:in `map'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/invocation
.rb:133:in `invoke_all'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/group.rb:2
32:in `dispatch'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/invocation
.rb:115:in `invoke'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/group.rb:2
77:in `block in _invoke_for_class_method'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/shell.rb:6
8:in `with_padding'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/group.rb:2
66:in `_invoke_for_class_method'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/group.rb:1
49:in `_invoke_from_option_orm'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/command.rb
:27:in `run'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/invocation
.rb:126:in `invoke_command'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/invocation
.rb:133:in `block in invoke_all'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/invocation
.rb:133:in `each'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/invocation
.rb:133:in `map'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/invocation
.rb:133:in `invoke_all'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/group.rb:2
32:in `dispatch'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.19.1/lib/thor/base.rb:44
0:in `start'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.2.0/lib/rails/genera
tors.rb:157:in `invoke'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.2.0/lib/rails/comman
ds/generate.rb:13:in `<top (required)>'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.2.0/lib/active_
support/dependencies.rb:274:in `require'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.2.0/lib/active_
support/dependencies.rb:274:in `block in require'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.2.0/lib/active_
support/dependencies.rb:240:in `load_dependency'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.2.0/lib/active_
support/dependencies.rb:274:in `require'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.2.0/lib/rails/comman
ds/commands_tasks.rb:123:in `require_command!'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.2.0/lib/rails/comman
ds/commands_tasks.rb:130:in `generate_or_destroy'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.2.0/lib/rails/comman
ds/commands_tasks.rb:50:in `generate'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.2.0/lib/rails/comman
ds/commands_tasks.rb:39:in `run_command!'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.2.0/lib/rails/comman
ds.rb:17:in `<top (required)>'
        from bin/rails:4:in `require'

2 个答案:

答案 0 :(得分:2)

  1. 请务必仔细检查您的gemfile。
  2. 我的Gemfile:

    gem 'mysql2', '~> 0.3'
    
    1. 安装捆绑包。

    2. 验证您的database.yml文件。

    3. database.yml中:

      development:
        adapter: mysql2
        encoding: utf8
        database: mydatabase
        username: myusername
        password: mypass123
        host: localhost
        pool: 5
        port: 3306
        timeout: 5000
      

答案 1 :(得分:2)

我遇到了同样的问题,而且我所能得到的是在使用rails 4.2.4的gem版本0.4.0上出现问题,我所做的就是去修复我的Gemfile并更改gem版本MySQL的:

gem 'mysql2'

对于这个

gem 'mysql2', '~> 0.3.20'

然后运行:

bundle install

它完美无缺

我希望它可能有用!