在现有项目上从sqlite3更改为mysql

在现有项目上从sqlite3更改为mysql,mysql,ruby-on-rails,Mysql,Ruby On Rails,我按照其他解决方案中的建议添加了gem mysql2,然后尝试创建模型,但我面临同样的错误 下面是我的database.yml文件和错误消息。另外,请注意,我正在遵循一个教程,其中我需要首先生成模型,然后运行db:migrate default: &default adapter: mysql database: myrubyblog username: root password: root123 pool: <%= ENV.fetch("RAILS_MAX_

我按照其他解决方案中的建议添加了gem mysql2,然后尝试创建模型,但我面临同样的错误

下面是我的database.yml文件和错误消息。另外,请注意,我正在遵循一个教程,其中我需要首先生成模型,然后运行db:migrate

default: &default
  adapter: mysql
  database: myrubyblog
  username: root
  password: root123
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  adapter: mysql
  database: myrubyblog
  username: root
  password: root123
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000
  # Warning: The database defined as "test" will be erased and
  # re-generated from your development database when you run "rake".
  # Do not set this db to the same as development or production.
test:
  adapter: mysql
  database: myrubyblog
  username: root
  password: root123
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

production:
  adapter: mysql
  database: myrubyblog
  username: root
  password: root123
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000
default:&default
适配器:mysql
数据库:myrubyblog
用户名:root
密码:root123
游泳池:
超时:5000
发展:
适配器:mysql
数据库:myrubyblog
用户名:root
密码:root123
游泳池:
超时:5000
#警告:定义为“测试”的数据库将被删除并删除
#运行“rake”时从开发数据库重新生成。
#请勿将此数据库设置为与开发或生产相同的数据库。
测试:
适配器:mysql
数据库:myrubyblog
用户名:root
密码:root123
游泳池:
超时:5000
制作:
适配器:mysql
数据库:myrubyblog
用户名:root
密码:root123
游泳池:
超时:5000

sania@sania-Swift-SF314-52:~/myrubyblog$rails g模型类别名称:字符串
/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active\u-support/dependencies.rb:292:在“require”中:无法加载“active\u-record/connection\u-adapters/mysql\u-adapter”。确保config/database.yml中的适配器有效。如果使用的适配器不是“mysql2”、“postgresql”或“sqlite3”,请将必要的适配器gem添加到gem文件中。(加载错误)
from/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active\u support/dependencies.rb:292:在'block in require'中
来自/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active\u support/dependencies.rb:258:在“加载\u依赖项”中
from/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active\u support/dependencies.rb:292:in'require'
来自/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.1.6/lib/active\u record/connection\u adapters/connection\u specification.rb:186:in“spec”
来自/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.1.6/lib/active\u record/connection\u adapters/abstract/connection\u pool.rb:880:“建立连接”中
from/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.1.6/lib/active\u record/connection\u handling.rb:58:在“建立连接”中
from/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.1.6/lib/active\u record/railtie.rb:124:in `块(2层)in'
来自/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active\u support/lazy\u load\u hooks.rb:69:in'instance\u eval'
来自/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active\u-support/lazy\u-load\u-hooks.rb:69:“执行中的块\u-hook”
from/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active\u support/lazy\u load\u hooks.rb:60:in`with_execution\u control'
来自/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active\u-support/lazy\u-load\u-hooks.rb:65:in'execute\u-hook'
来自/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active\u-support/lazy\u-load\u-hooks.rb:50:in'block in-in-run\u-load\u-hooks'
from/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active\u-support/lazy\u-load\u-hooks.rb:49:in'each'
来自/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active\u-support/lazy\u-load\u-hooks.rb:49:in'run\u-load\u-hooks'
来自/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.1.6/lib/active\u record/base.rb:326:in`'
from/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.1.6/lib/active\u record/base.rb:25:in`'
from/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active\u support/dependencies.rb:292:in'require'
from/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active\u support/dependencies.rb:292:在'block in require'中
来自/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active\u support/dependencies.rb:258:在“加载\u依赖项”中
from/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active\u support/dependencies.rb:292:in'require'
来自/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:356:在“活动记录配置”中
来自/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:273:在“断开与数据库的连接”中
from/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:107:在“预加载”中
from/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:153:in'serve'
from/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:141:在“运行中的块”中
from/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:135:在“循环”中
from/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:135:在“运行”中
来自/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application/boot.rb:19:in`'
from/home/sania/.rbenv/versions/2.5.0/lib/ruby/2.5.0/rubygems/core\u ext/kernel\u require.rb:59:in'require'
from/home/sania/.rbenv/versions/2.5.0/lib/ruby/2.5.0/rubygems/core\u ext/kernel\u require.rb:59:in'require'
from-e:1:in`'

您必须在
config/database.yml
文件中有一行代码来指定适配器是
mysql2
而不是
mysql

adapter:mysql2
而不是
adapter:mysql

如果您安装并捆绑
mysql2sania@sania-Swift-SF314-52:~/myrubyblog$ rails g model category name:string
/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292: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 /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.1.6/lib/active_record/connection_adapters/connection_specification.rb:186:in `spec'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.1.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:880:in `establish_connection'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.1.6/lib/active_record/connection_handling.rb:58:in `establish_connection'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.1.6/lib/active_record/railtie.rb:124:in `block (2 levels) in <class:Railtie>'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:69:in `instance_eval'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:69:in `block in execute_hook'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:60:in `with_execution_control'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:65:in `execute_hook'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:50:in `block in run_load_hooks'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:49:in `each'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:49:in `run_load_hooks'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.1.6/lib/active_record/base.rb:326:in `<module:ActiveRecord>'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.1.6/lib/active_record/base.rb:25:in `<top (required)>'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:356:in `active_record_configured?'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:273:in `disconnect_database'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:107:in `preload'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:153:in `serve'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:141:in `block in run'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:135:in `loop'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:135:in `run'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application/boot.rb:19:in `<top (required)>'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    from /home/sania/.rbenv/versions/2.5.0/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    from -e:1:in `<main>'
default: &default
  adapter: mysql2
  username: root
  password: root123
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: myrubyblog_dev

test:
  <<: *default
  database: myrubyblog_test

production:
  <<: *default
  database: myrubyblog_prod