Please Install ActiveRecord MySQL2 Adapter
I recently got this error on a Rails 3 application I am working on:
% rake assets:precompile RAILS_ENV=production rake aborted! LoadError: Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (can't activate mysql2 (~> 0.3.10), already activated mysql2-0.4.1. Make sure all dependencies are added to Gemfile.)
Since it is the second time I had to solve it (and I forgot how I did it the first time), here are the causes and the solution for future reference … if I remember I wrote this post.
In synthesis: the problem seems to be caused by using a version of mysql2 gem above 0.4.0 with Rails 3.
- After having updated MySQL, my Rails 3 application refused to start,
because the bundled
mysql2gem was compiled for the version of MySQL installed previously. Fair enough: the gem has some native extensions which depend on the version of MySQL being used.
- I run
bundle update, which installed a new version of the MySQL gem (version 0.4.1). The installed version is compatible with the new version of MySQL. The command also updated the references in the bundle.
- Bad idea! …
rake assets:precompilestarted complaining about the wrong version of MySQL being used (see error above)
bundle updatedoes not help (short of killing time and breaking patience)
- Setting by hand the correct version of
mysql2in the Gemfile does not help (we go back to the initial problem)
I reinstalled the
gem uninstall mysql2 -v 0.3.20 gem install mysql2 -v 0.3.20
The point is that the native extensions of the gem are re-compiled based the current version of
mysql2installed on the system.
I then edited the Gemfile so that the application uses the correct version of the Gem:
gem "mysql2", "~> 0.3.20"
- I run
bundle updateto update the references and everything started working again.
… what was the problem, again?
- After updating MySQL, re-install the