Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set MySQL as the default database in Rails 3?

I started using Rails 2 last April but stopped this June because I thought learning it when Rails 3 was released would be more practical since a lot of it was completely refactored and restructured. I used to work with Ubuntu 10.04 (with SQLite3 as the default db) but now I'm using Windows 7 and MySQL 5. I already installed the gem adapter for MySQL, but to use it I still need to tweak database.yml. Thanks.

like image 628
arscariosus Avatar asked Sep 01 '10 11:09

arscariosus


People also ask

What database does Rails use by default?

Rails defaults to using a SQLite database when creating a new project, but you can always change it later.

Can you use MySQL with Rails?

Now your Rails applications can use MySQL databases.


2 Answers

In terms of database configuration, nothing much has really changed between Rails 2 and 3 with the exception of how you load your MySQL driver. This used to be done in config/environment.rb but is now done in Gemfile:

gem 'mysql'

The default config/database.yml file is set up with SQLite, but you can easily change this over to be MySQL. A generic version looks like:

defaults: &defaults
  adapter: mysql
  username: localdev
  password: mylocaldevpasswordwhateveritis
  host: localhost

development:
  <<: *defaults
  database: project_dev

test:
  <<: *defaults
  database: project_test

It's the adapter declaration line that sets what driver to use.

like image 72
tadman Avatar answered Oct 15 '22 21:10

tadman


In tadman's answer, use gem 'mysql2' for the rails 3 since rails 3 now uses the new mysql adapter !!

like image 26
Ishu Avatar answered Oct 15 '22 22:10

Ishu