简体   繁体   English

LoadError:加载“mysql2”Active Record 适配器时出错 [rails 5.2.3]

[英]LoadError: Error loading the 'mysql2' Active Record adapter [rails 5.2.3]

My rails app is using ruby 2.6.3, rails 5.2.3, and MySQL version is 5.7.27.我的 rails 应用程序使用的是 ruby 2.6.3、rails 5.2.3 和 MySQL 版本是 5.7.27。 When I tried to run rake db:create then rails showed me the error:当我尝试运行rake db:create时,rails 向我显示了错误:

rails aborted!
LoadError: Error loading the 'mysql2' Active Record adapter. Missing a gem it depends on? cannot load such file -- /path/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/mysql2_adapter.rb
bin/rails:4:in `<main>'

Caused by:
LoadError: cannot load such file -- /path/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/mysql2_adapter.rb
bin/rails:4:in `<main>'
Tasks: TOP => db:create => db:load_config
(See full trace by running task with --trace)

this is my Gemfile:这是我的 Gemfile:

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

ruby '2.6.3'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '5.2.3'
# Use mysql as the database for Active Record
gem 'mysql2', '0.5.2'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# 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'

# 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.1.0', require: false

# authentication
gem 'devise'
gem 'devise-i18n'

# admin
gem 'administrate'

gem 'haml-rails'
gem 'bootstrap'
gem 'jquery-rails'

group :development, :test do
  gem 'rspec-rails'
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'better_errors'
  gem 'pry-byebug'
  gem 'pry-rails'
  gem 'web-console', '>= 3.3.0'
  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'
  gem 'rubocop', require: false
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of chromedriver to run system tests with Chrome
  gem 'chromedriver-helper'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

and this is my db configuration:这是我的数据库配置:

# MySQL. Versions 5.1.10 and up are supported.
#
# Install the MySQL driver
#   gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
#   gem 'mysql2'
#
# And be sure to use new-style password hashing:
#   https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html
#
default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: name
  password: password
  host: localhost
  port: 3306

development:
  <<: *default
  database: project_development

# 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: project_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="mysql2://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>
#
production:
  <<: *default
  database: project_production
  username: videomeeting
  password: <%= ENV['PROJECT_DATABASE_PASSWORD'] %>

I also checked from the gem list and saw that mysql2 already installed.我还检查了 gem 列表,发现已经安装了mysql2 Do you have any suggestion with this error?你对这个错误有什么建议吗? Thanks in advance.提前致谢。

通过重新安装activerecord gem 修复了这个问题。

我在使用 postgresql 时遇到了类似的问题,并且执行“gem install pg”对我有用。

Had this issue a while back, uninstall the mysql gem via the前段时间有这个问题,通过卸载mysql gem

bundle exec gem uninstall mysql2

and uninstall MySQL on your PC via并通过以下方式在您的 PC 上卸载 MySQL

brew uninstall mysql@5.7

Re-install both:重新安装两者:

brew install mysql@5.7

gem install mysql2 -v '0.5.2'

and hopefully it'll work for you as it did for me.希望它对你有用,就像对我一样。 I still however had to figure out the appropriate configurations as you have here:但是,我仍然必须像您在这里一样找出适当的配置:

  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: name
  password: password
  host: localhost
  port: 3306

Because I wanted to connect my rails app to a MySQL running on Docker, I set my configurations to:因为我想将我的 rails 应用程序连接到在 Docker 上运行的 MySQL,所以我将配置设置为:

db_database: 'cs_development'
db_username: ''
db_password: 'MySQL'
db_host: '127.0.0.1'
db_port: '3306'
db_adapter: 'mysql2'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM