mysql - 在现有项目上从 sqlite3 更改为 mysql
问题描述
我按照其他解决方案中的建议添加了 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
sania@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>'
解决方案
您必须在config/database.yml
文件中有一行来指定适配器是mysql2
和不是mysql
。
adapter: mysql2
代替adapter: mysql
如果您的安装和捆绑没问题mysql2
,它将起作用。
您可能需要重新安装mysql2
. 您可以使用以下命令执行此操作
gem "mysql2"
bundle install
推荐阅读
- tomcat - Tomcat - 异步请求的 WELD 解决方法
- php - PHP DOMDocument AppendXML 返回空(特殊字符)
- reactjs - 如何在 react-native-share 中使 url 字符串可点击
- sql - 如何选择多个列,但仅显示唯一/非重复的结果(基于一个特定列)?
- javascript - 如何限制 chartIQ 库中的最小和最大缩放
- c - 如何在 C 中获取操作系统信息
- elasticsearch - 如何将 not_analyzed 应用于字段
- reactjs - 如何在带有 MDXJS 的 MDX / Markdown 文件中使用 i18next / react-i18next?
- ios - 如何在 Swift 中向响应式网站添加后退按钮
- javascript - 将多个对象数组转换为单个数组,同时合并公共键上的对象