简体   繁体   中英

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. When I tried to run rake db:create then rails showed me the error:

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:

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. 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

bundle exec gem uninstall mysql2

and uninstall MySQL on your PC via

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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