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.

Symptoms:

  1. After having updated mysql, my Rails 3 application refused to start, because the bundled mysql2 gem 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.
  2. 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.
  3. Bad idea! … rake assets:precompile started complaining about the wrong version of mysql being used (see error above)

Attempts:

  1. Running bundle update does not help (short of killing time and breaking patience)
  2. Setting by hand the correct version of mysql2 in the Gemfile does not help (we go back to the initial problem)

Solution:

  1. I reinstalled the mysql2 0.3.20 gem:

    ~~~ sh
    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 mysql2 installed on the system.

  2. I then edited the Gemfile so that the application uses the correct version of the Gem:

    ~~~ sh
    gem "mysql2", "~> 0.3.20"
    ~~~
    
  3. I run bundle update to update the references and everything started working again.

… what was the problem, again?

Next Time:

  1. After updating mysql, re-install the mysql2 gem.