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.
4 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.

- 208,517
- 23
- 234
- 262
-
Is there a way to make mysql the default instead of sqlite? – Jason Noble Sep 01 '10 at 14:25
-
For new projects? Not easily. This is a core feature of Rails. Typically people prepare a "skeleton" project for their new applications that's customized as required, with database drivers, plugins, gems all set up as you like them, then clone it for new projects. That way you can have any defaults you like, no matter how exotic. – tadman Sep 01 '10 at 15:00
-
Thanks. So I cannot make it default for new projects. – arscariosus Sep 01 '10 at 15:31
-
8You can pass `--database=mysql` to the `rails new` command (or perhaps alias it in your shell - since on windows, maybe wrap the command in a batch of cmd script). Alternatively, you can create an application template: http://railscasts.com/episodes/148-app-templates-in-rails-2-3 – Brian Sep 01 '10 at 16:12
In tadman's answer, use gem 'mysql2' for the rails 3 since rails 3 now uses the new mysql adapter !!

- 628
- 1
- 13
- 28
You can change rails to default to MySql when you generate a new application, but you have to edit a line in your rails installation. You'll have to make the change to every version, and every time you update the rails gem.
I use Ruby-Enterprise. So here's what I do:
In file (where 1.8 is the ruby version and 3.0.4 is the rails version):
/opt/ruby-enterprise/lib/ruby/gems/1.8/gems/railties-3.0.4/lib/rails/generators/rails/app/app_generator.rb
Edit: In rails-3.1.0-rc1 the file is:
gems/railties-3.1.0.rc1/lib/rails/generators/app_base.rb
Search for this line:
class_option :database, :type => :string, :aliases => "-d", :default => "sqlite3",
Change "sqlite3" to "mysql".
class_option :database, :type => :string, :aliases => "-d", :default => "mysql",
So instead of doing:
rails new application_name -d mysql
I can just do (and the database.yml and Gemfiles are configured for the mysql2 gem):
rails new application_name
This assumes you have the correct mysql2 gem installed already. Also, I've only been doing this since Rails 3 came out. It's probably similar for previous versions. Again, every time you update Rails, you'll have to find and edit that file.

- 2,410
- 1
- 19
- 29
-
1I'm using RVM with Ruby 1.9.2 and Rails 3, and the path to the file for me is found at: $(rvm gemdir)/gems/railties-3.0.5/lib/rails/generators/rails/app/app_generator.rb. The instructors work perfectly. – gunit888 Mar 30 '11 at 14:49
Since Rails 3.2 you can define a .railsrc file with custom command line options that will always apply to rails new
So, if you create a file called .railsrc
and put it in your home directory w/ the contents like this -d mysql
it wll make mysql be your default database. You can put any of the command line options in there (including application templates which are supper awesome!)
Run rails new --help
from the command line to see all your options.

- 8,329
- 4
- 36
- 33