rails 5 mysql 适配器问题

rails 5 mysql adapter issue

我正在尝试在 rails 5 中创建模型并已安装 mysql-community-server。我还安装了 mysql 和 mysql2 gem。但是当我 运行 rails generate model Book 低于错误时,

[root@server-linux library]# rails generate model Book
/root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:in `require': Could not load 'active_record/connection_adapters/mysql_adapter'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile. (LoadError)
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:in `block in require'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:259:in `load_dependency'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:in `require'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/connection_handling.rb:53:in `establish_connection'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/railtie.rb:125:in `block (2 levels) in <class:Railtie>'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/lazy_load_hooks.rb:44:in `each'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/base.rb:324:in `<module:ActiveRecord>'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/base.rb:24:in `<top (required)>'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:338:in `active_record_configured?'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:259:in `disconnect_database'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:97:in `preload'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:143:in `serve'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:131:in `block in run'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `loop'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `run'
        from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in `<top (required)>'
        from /root/.rbenv/versions/2.2.3/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /root/.rbenv/versions/2.2.3/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from -e:1:in `<main>'
[root@server-linux library]#

database.yaml:

[root@server-linux library]# cat config/database.yml
development:
  adapter: mysql2
  database: library_development
  username: root
  password: [password]
  host: localhost

test:
  adapter: mysql2
  database: library_test
  username: root
  password: [password]
  host: localhost

production:
  adapter: mysql2
  database: library_production
  username: root
  password: [password]
  host: localhost

[root@server-linux library]#

版本详情:

[root@server-linux library]# rails -v
Rails 5.0.0
[root@karthick-linux library]# gem list | grep mysql
activerecord-mysql-adapter (0.0.1)
mysql (2.9.1)
mysql2 (0.4.4)
[root@server-linux library]#

我什至使用命令 rails new library -d mysql

创建了应用程序

知道为什么 mysql 适配器连接失败。

检查您的 Gem 文件

在您的 gem 文件中找到 gem 'mysql2'

如果找不到请添加 gem 'mysql2' 然后 运行 bundle install 并重新启动服务器。

您是否尝试安装 bundler。我猜测 mysql2 gem 是为不同版本的 ruby 安装的,而不是你当前版本的 ruby。先尝试安装 bundler

gem install bundler
bundle install   
rails db:migrate
rails g model book

您也不需要 mysql gem,您可以删除它并在 database.yml 文件中使用 mysql2 作为适配器。