Rails 4 - Gem::LoadError: Specified 'mysql2' for database adapter, but the gem is not loaded

前端 未结 9 878
一整个雨季
一整个雨季 2020-12-02 06:13

In my gemfile I have:

gem \'mysql2\'

My database.yml is as follows:

default: &default
  adapter: mysql2
  database: <         


        
相关标签:
9条回答
  • 2020-12-02 06:34

    If you are able to upgrade your rails version, then change your Gemfile to this and it will solve the problem without downgrading the mysql2 gem version:

    gem 'rails', '4.2.6'
    
    0 讨论(0)
  • 2020-12-02 06:36

    The answer to this particular question relative to when it was posted and the version of Rails being used is that the problem is caused from doing a bundle update and your mysql2 version updates to 0.4.x which has an incompatibility issue with latest Rails ActiveRecord.

    Again, please note this is NOT the solution for people using older versions of Rails / ActiveRecord.

    The quick solution is to simply specify the mysql2 version in your gemfile as follows:

    gem 'mysql2', '0.3.20'

    The long solution would be to wait for either an update to ActiveRecord or something in mysql2 to change.

    0 讨论(0)
  • 2020-12-02 06:38

    This usually happens when you are missing some mysql packages on your machine. Do you get any errors from gem install mysql2? What OS are you working on?

    If on debian or ubuntu try sudo apt-get install libmysqlclient-dev.

    Also make sure that the gem is not placed inside a group statement in your Gemfile.

    0 讨论(0)
  • 2020-12-02 06:38

    try this:

    bundle update mysql2
    

    this command will update your 'mysql2' gem to the latest version (should be 0.3.17 or higher) and start your rails server.

    0 讨论(0)
  • 2020-12-02 06:42

    Just do:

    gem 'mysql2', '~> 0.3.18' this gem works with rails version 4.x.x

    if install gem 'mysql2', '~> 0.4.0' it produces gem load error and causes compatibility issues

    0 讨论(0)
  • 2020-12-02 06:46

    This issue was addressed here: https://github.com/brianmario/mysql2/issues/950

    For Rails 4.x please pin the gem to mysql2 '~> 0.4.0' to avoid the 0.5.x upgrade.

    Gemfile:

    gem 'rails', '4.2.8'
    gem 'mysql2', '~> 0.4.0'
    

    Then run bundle update rails mysql2

    I am currently using mysql v 8.0.11

    0 讨论(0)
提交回复
热议问题