Archive for the ‘Ruby’ Category

Rails 3 and mysql2 gem issues

Wednesday, February 1st, 2012

While experimenting with Rails for a possible use in my next project. I was setting up the database and came across an error when I ran rake db:create; “Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (mysql2 is not part of the bundle. Add it to Gemfile.)”

If you try to install the gem activerecord-mysql2-adapter You will get:

ERROR:  Could not find a valid gem 'activerecord-mysql2-adapter' (>= 0) in any repository

The reason behind this error is that the mysql2 0.3 gem isn’t compatible with Rails 3.0. The workaround that I found to work is to use the mysql2 0.2.7 gem.

gem install mysql2 -v 0.2.7

And add this to your Gemfile:

gem 'mysql2', '< 0.3'

If you have the other mysql2 gem files you can uninstall it by gem uninstall mysql2.

Sample mysql config in config/database.yml:

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: database_name
  pool: 5
  username: database_username
  password: database_password
  socket: /var/run/mysqld/mysqld.sock

I am on a Debian system so the mysql socket is located in /var/run/mysqld/mysqld.sock, this could be different for your system.