0

自从创建了一个不同的 rails 项目以来,我在每个项目中都遇到了这个 mysql 要求错误。

我试过用捆绑器安装'mysql'和'mysql2'(即使我使用的是postgres)gem。我尝试过不同的 ruby​​ 版本、不同的 rails 版本、不同的 bootsnap 版本,我从 rvm 切换到 rbenv。我的 Rails 项目都无法运行。

bootsnap 错误显示如下。/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require': 无法加载 'mysql' Active Record 适配器。确保适配器在 config/database.yml 中拼写正确,并且您已将必要的适配器 gem 添加到 Gemfile。(加载错误)

这是完整的追溯

/home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/app_loader.rb:53: warning: Insecure world writable dir /home/johan/.rbenv/versions in PATH, mode 040777
/home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22: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)
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/loaded_features_index.rb:89:in `register'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:40:in `rescue in require'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:26:in `require'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `block in require'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in `load_dependency'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `require'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:191:in `spec'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:956:in `establish_connection'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:60:in `establish_connection'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/railtie.rb:136:in `block (2 levels) in <class:Railtie>'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:71:in `instance_eval'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:71:in `block in execute_hook'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:62:in `with_execution_control'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:67:in `execute_hook'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:43:in `block in on_load'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:42:in `each'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:42:in `on_load'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/railtie.rb:132:in `block in <class:Railtie>'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/initializable.rb:32:in `instance_exec'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/initializable.rb:32:in `run'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/initializable.rb:61:in `block in run_initializers'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/2.5.0/tsort.rb:228:in `block in tsort_each'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/2.5.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/2.5.0/tsort.rb:431:in `each_strongly_connected_component_from'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/2.5.0/tsort.rb:349:in `block in each_strongly_connected_component'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/2.5.0/tsort.rb:347:in `each'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/2.5.0/tsort.rb:347:in `call'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/2.5.0/tsort.rb:347:in `each_strongly_connected_component'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/2.5.0/tsort.rb:226:in `tsort_each'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/2.5.0/tsort.rb:205:in `tsort_each'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/initializable.rb:60:in `run_initializers'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/application.rb:361:in `initialize!'
    from /mnt/c/users/j_tel/bin/fancapade-rails/config/environment.rb:5:in `<top (required)>'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `block in require'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in `load_dependency'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `require'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:48:in `require_relative'
    from config.ru:3:in `block in <main>'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/rack-2.0.7/lib/rack/builder.rb:55:in `instance_eval'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/rack-2.0.7/lib/rack/builder.rb:55:in `initialize'
    from config.ru:in `new'
    from config.ru:in `<main>'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/rack-2.0.7/lib/rack/builder.rb:49:in `eval'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/rack-2.0.7/lib/rack/builder.rb:49:in `new_from_string'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/rack-2.0.7/lib/rack/builder.rb:40:in `parse_file'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/rack-2.0.7/lib/rack/server.rb:319:in `build_app_and_options_from_config'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/rack-2.0.7/lib/rack/server.rb:219:in `app'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/commands/server/server_command.rb:27:in `app'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/rack-2.0.7/lib/rack/server.rb:354:in `wrapped_app'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/commands/server/server_command.rb:89:in `log_to_stdout'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/commands/server/server_command.rb:51:in `start'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/commands/server/server_command.rb:147:in `block in perform'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/commands/server/server_command.rb:142:in `tap'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/commands/server/server_command.rb:142:in `perform'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/command.rb:27:in `run'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/invocation.rb:126:in `invoke_command'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor.rb:387:in `dispatch'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/command/base.rb:65:in `perform'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/command.rb:46:in `invoke'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/commands.rb:18:in `<top (required)>'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `block in require'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in `load_dependency'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `require'
    from /mnt/c/users/j_tel/bin/fancapade-rails/bin/rails:9:in `<top (required)>'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `load'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/client.rb:30:in `run'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/spring-2.1.0/bin/spring:49:in `<top (required)>'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    from /home/johan/.rbenv/versions/2.5.5/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    from /mnt/c/users/j_tel/bin/fancapade-rails/bin/spring:15:in `<top (required)>'
    from bin/rails:3:in `load'
    from bin/rails:3:in `<main>'
=> Booting Puma
=> Rails 5.2.3 application starting in development 
=> Run `rails server -h` for more startup options
Exiting

这是宝石文件

source 'https://rubygems.org'

git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.5'
group :development do
  gem 'pg'
end
group :production do
  gem 'pg'
end

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'

gem 'rails', '~> 5.2.1'

# Use sqlite3 as the database for Active Record
# Use Puma as the app server

gem 'puma', '~> 3.11'

gem 'twilio-ruby', '~> 5.16.0'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder

# gem 'jbuilder', '~> 2.5'

# Use Redis adapter to run Action Cable in production

# gem 'redis', '~> 4.0'

# Use ActiveModel has_secure_password

# gem 'bcrypt', '~> 3.1.7'

gem 'geocoder'

gem 'textacular', '~> 5.0'

# Use ActiveStorage variant

# gem 'mini_magick', '~> 4.8'

# Use Capistrano for deployment

# gem 'capistrano-rails', group: :development

# Reduces boot times through caching; required in config/boot.rb

gem 'bootsnap', '1.4.4'

gem 'devise_token_auth'

gem 'fog-aws'

gem 'omniauth'
gem 'mysql2', require: false
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible

gem 'rack-cors'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console

  gem 'byebug', platforms: %i[mri mingw x64_mingw]
end

gem 'seed_dump'

group :development do
  gem 'listen', '>= 3.0.5', '< 3.2'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring

  gem 'spring'

  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem

gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]

gem 'bcrypt', '~> 3.1'

# gem 'geokit-rails', '~> 2.3'

gem 'devise', '~> 4.4'

gem 'carrierwave', '~> 1.2'

gem "stripe", "~> 4.12"

# gem "mysql", "~> 2.9"

# gem "mysql2", "~> 0.5.2"


和 database.yml

# cat config/database.yml
# PostgreSQL. Versions 9.1 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On OS X with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  timeout: 5000
  database: fancapade
  username: postgres
  password: <%= ENV['RAILS_DEV_PASSWD'] %>
  # port: 5432
  # database: fancapade
  # username: johan
  # host: localhost
  # password: <%= ENV['RAILS_DEV_PASSWD'] %>
  # # password:<%= ENV['RAILS_DEV_PASSWD'] %>
  # The specified database role being used to connect to postgres.
  # To create additional roles in postgres see `$ createuser --help`.
  # When left blank, postgres will use the default role. This is
  # the same name as the operating system user that initialized the database.
  #username: myapp
  # The password associated with the postgres role (username).
  #password:
  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  #host: localhost
  # The TCP port the server listens on. Defaults to 5432.
  # If your server runs on a different port number, change accordingly.
  #port: 5432
  # Schema search path. The server defaults to $user,public
  #schema_search_path: myapp,sharedapp,public
  # Minimum log levels, in increasing order:
  #   debug5, debug4, debug3, debug2, debug1,
  #   log, notice, warning, error, fatal, and panic
  # Defaults to warning.
  #min_messages: notice

# 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:
  <<: *default
  database: myapp_test

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>
#
production:
  <<: *default
  database: myapp_production
  username: myapp
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

4

2 回答 2

0

尝试更新 ruby​​ 版本并在 Gemefile 添加“gem 'mysql2', require: false”

PS如果问题仍然存在,请尝试更新捆绑器,而不是捆绑安装

于 2019-09-12T17:14:00.160 回答
0

你的意图是使用pg还是mysql2

无论哪种方式,请检查您是否设置ENV['DATABASE_URL']错误。由于这发生在不同的项目中,我猜你已经在你的 shell 中定义了它。

Rails 从您的 database.yml 加载访问数据库所需的信息,但也可以从该 env 变量中获取该信息。

于 2019-09-12T18:51:22.197 回答