Trouble getting Rails 3 to work with MySQL

philfreo picture philfreo · May 15, 2011 · Viewed 7.4k times · Source

Brand new at Rails. I had a simple Rails 3 app on OS X 10.6 going, using the default sqlite3 database.

I'm trying to convert that to use the MySQL server that I already have installed. (mysql5 -uroot works.) I'd like to use mysql2, which I installed and it said it was successful.

database.yml:

development:
  adapter: mysql2
  database: myproject
  username: root
  socket: /tmp/mysql.sock

Gemfile:

source 'http://rubygems.org'
gem 'rails', '3.0.6'
gem 'mysql2'
gem 'sqlite3'

More info:

$  bundle show mysql2
/Users/me/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.3.2

The page loads with: "ActiveRecord::ConnectionNotEstablished"

I'm not sure if I'm supposed to create the myproject database myself manually or not. Regardless, I've tried it both ways.

I get:

$ rake db:schema:load
(in /Users/me/Sites/myproject/web)
WARNING: This version of mysql2 (0.3.2) doesn't ship with the ActiveRecord adapter bundled anymore as it's now part of Rails 3.1
WARNING: Please use the 0.2.x releases if you plan on using it in Rails <= 3.0.x
-- create_table("notes", {:force=>true})
rake aborted!
Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (no such file to load -- active_record/connection_adapters/mysql2_adapter)

I have tried to install that gem:

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

From reading some very similar questions, it seems what solves the problem for everyone else is just making sure the database.yml adapter says mysql2. I've already done this though and it didn't help...

Answer

Zabba picture Zabba · May 15, 2011

Use this in Gemfile:

gem 'mysql2','0.2.7'

And then bundle install.

Note that mysql2 version 0.3.x is for Rails 3.1.x. For Rails 3.0.x, use the 0.2.x version of mysql2.

Ps. If you're not already using rvm, I highly recommend using it. See here for why rvm is so cool.