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

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


Just a further update - the solution in the question is correct.

The 4th comment is worth taking note of:

This isn't a bug with mysql2, it's a problem with the requirement in the ActiveRecord adapter: http://github.com/rails/rails/issues/21544. This is fixed in rails master: https://github.com/rails/rails/commit/5da5e3772c32593ecf2f27b8865e81dcbe3af692

I was able to tie Rails 4.2.4 to the 4-2-stable branch and get it working with the latest mysql2:

enter image description here

gem 'rails', '~> 4.2.4', git: "git://github.com/rails/rails.git", branch: '4-2-stable'
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw] #-> Rails 4.1+

#DB
gem 'mysql2'

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