我在 Heroku 上运行此应用程序,
我正在尝试在运行 Rails 6.1.7 的应用程序上将数据库从 ClearDb 更改为 JawsDB(都是 MySql 托管站点)
由于某种原因连接不成功,我收到以下错误
我的数据库版本:
MySql 版本 8.0.35
捆绑显示 mysql2 -> /app/vendor/bundle/ruby/3.1.0/gems/mysql2-0.5.5
我有 DB_ADAPTER 作为 mysql - 也尝试过 mysql2
当我执行此命令时 ** bundle execrails c **
它发出这样的信息:
/app/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require': Could not load the 'mysql' Active Record adapter. Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary adapter gem to your Gemfile. (LoadError)
这是应用程序启动错误:
653342+00:00 app[web.1]: => Rails 6.1.7 application starting in production
653351+00:00 app[web.1]: => Run `bin/rails server --help` for more startup options
815587+00:00 app[web.1]: Exiting
815645+00:00 app[web.1]: /app/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require': Could not load the 'mysql' Active Record adapter. Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary adapter gem to your Gemfile. (LoadError)
815646+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/zeitwerk-2.6.7/lib/zeitwerk/kernel.rb:38:in `require'
815647+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7/lib/active_support/dependencies.rb:332:in `block in require'
815647+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7/lib/active_support/dependencies.rb:299:in `load_dependency'
815647+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7/lib/active_support/dependencies.rb:332:in `require'
815648+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:1205:in `resolve_pool_config'
815648+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:1046:in `establish_connection'
815648+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.7/lib/active_record/connection_handling.rb:52:in `establish_connection'
815648+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/activerecord-import-1.4.1/lib/activerecord-import/import.rb:252:in `establish_connection'
815649+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.7/lib/active_record/railtie.rb:222:in `block (2 levels) in <class:Railtie>'
815649+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7/lib/active_support/lazy_load_hooks.rb:71:in `class_eval'
815649+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7/lib/active_support/lazy_load_hooks.rb:71:in `block in execute_hook'
815649+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7/lib/active_support/lazy_load_hooks.rb:61:in `with_execution_control'
815650+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7/lib/active_support/lazy_load_hooks.rb:66:in `execute_hook'
815651+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7/lib/active_support/lazy_load_hooks.rb:43:in `block in on_load'
815651+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7/lib/active_support/lazy_load_hooks.rb:42:in `each'
815651+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7/lib/active_support/lazy_load_hooks.rb:42:in `on_load'
815651+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.7/lib/active_record/railtie.rb:217:in `block in <class:Railtie>'
815652+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/railties-6.1.7/lib/rails/initializable.rb:32:in `instance_exec'
815652+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/railties-6.1.7/lib/rails/initializable.rb:32:in `run'
815653+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/railties-6.1.7/lib/rails/initializable.rb:61:in `block in run_initializers'
815660+00:00 app[web.1]: from /app/vendor/ruby-3.1.3/lib/ruby/3.1.0/tsort.rb:228:in `block in tsort_each'
815660+00:00 app[web.1]: from /app/vendor/ruby-3.1.3/lib/ruby/3.1.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
815660+00:00 app[web.1]: from /app/vendor/ruby-3.1.3/lib/ruby/3.1.0/tsort.rb:431:in `each_strongly_connected_component_from'
815660+00:00 app[web.1]: from /app/vendor/ruby-3.1.3/lib/ruby/3.1.0/tsort.rb:349:in `block in each_strongly_connected_component'
815662+00:00 app[web.1]: from /app/vendor/ruby-3.1.3/lib/ruby/3.1.0/tsort.rb:347:in `each'
815663+00:00 app[web.1]: from /app/vendor/ruby-3.1.3/lib/ruby/3.1.0/tsort.rb:347:in `call'
815666+00:00 app[web.1]: from /app/vendor/ruby-3.1.3/lib/ruby/3.1.0/tsort.rb:347:in `each_strongly_connected_component'
815666+00:00 app[web.1]: from /app/vendor/ruby-3.1.3/lib/ruby/3.1.0/tsort.rb:226:in `tsort_each'
815667+00:00 app[web.1]: from /app/vendor/ruby-3.1.3/lib/ruby/3.1.0/tsort.rb:205:in `tsort_each'
815668+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/railties-6.1.7/lib/rails/initializable.rb:60:in `run_initializers'
815677+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/railties-6.1.7/lib/rails/application.rb:391:in `initialize!'
815677+00:00 app[web.1]: from /app/config/environment.rb:5:in `<main>'
815678+00:00 app[web.1]: from config.ru:3:in `require_relative'
815678+00:00 app[web.1]: from config.ru:3:in `block in <main>'
815679+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.6.4/lib/rack/builder.rb:116:in `eval'
815679+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.6.4/lib/rack/builder.rb:116:in `new_from_string'
815679+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.6.4/lib/rack/builder.rb:105:in `load_file'
815680+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.6.4/lib/rack/builder.rb:66:in `parse_file'
815681+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.6.4/lib/rack/server.rb:349:in `build_app_and_options_from_config'
815682+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.6.4/lib/rack/server.rb:249:in `app'
815685+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.6.4/lib/rack/server.rb:422:in `wrapped_app'
815686+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.6.4/lib/rack/server.rb:312:in `block in start'
815687+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.6.4/lib/rack/server.rb:379:in `handle_profiling'
815687+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.6.4/lib/rack/server.rb:311:in `start'
815688+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/railties-6.1.7/lib/rails/commands/server/server_command.rb:39:in `start'
815696+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/railties-6.1.7/lib/rails/commands/server/server_command.rb:144:in `block in perform'
815696+00:00 app[web.1]: from <internal:kernel>:90:in `tap'
815697+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/railties-6.1.7/lib/rails/commands/server/server_command.rb:135:in `perform'
815697+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
815697+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
815697+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
815698+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/railties-6.1.7/lib/rails/command/base.rb:69:in `perform'
815699+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/railties-6.1.7/lib/rails/command.rb:48:in `invoke'
815707+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/railties-6.1.7/lib/rails/commands.rb:18:in `<main>'
815707+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
815707+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
815707+00:00 app[web.1]: from bin/rails:4:in `<main>'
1。配置 Gemfile
默认情况下,RoR 附带 sqlite3。编辑 Gemfile 并注释/删除 sqlite3 条目并放置以下条目:
gem 'mysql2'
接下来,运行以下命令:
$bundle install
2。使用数据库连接设置配置database.yml文件
根据您的环境设置编辑
config/database.yml
文件。下面我们使用了 3 个环境——开发、测试和生产
development:
adapter: mysql2
database: developmentdb
username:
password:
host: localhost
encoding: utf8
test:
database: testdb
adapter: mysql2
encoding: utf8
username:
password:
host: localhost
production:
adapter: mysql2
database: Railsdb
username:
password:
host: localhost
encoding: utf8