Generating a Rails project configured to use MySQL

Published on March 13, 2009 and tagged with mysql  ruby on rails

A little trap I blundered into each time I generated a Rails project with

$ rails example

is that by default a generated project is configured to use a SQLite database. And so the configuration file for the databases looks as shown below (I omit the configurations for the “test” and “production” environments, as they are almost identical):

# example/config/database.yml
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

This leads, of course, to an error as soon as you try to access the database while there is no such driver installed ;-)

To avoid this small problem, you have to specify the database type when using the “rails” command:

$ rails -d mysql example

This command then generates the following database.yml:

# example/config/database.yml
development:
  adapter: mysql
  encoding: utf8
  database: example_development
  pool: 5
  username: root
  password:
  socket: /var/run/mysqld/mysqld.sock

Happy Rails baking!

Update 2009-03-23: An alternative approach is to define the variable RAILS_DEFAULT_DATABASE with:

$ export RAILS_DEFAULT_DATABASE=mysql

6 comments baked

  • Andreas March 15, 2009 at 12:37

    When I started learning rails I used SQLite for the development. Works just fine. But there are certain pitfalls with SQLite on your development machine when you’re going to use MySQL in production:
    If you use boolean values they are handled differently in SQLite and MySQL (That’s not a big problem because rails handles that for you – unless you use some complex queries as i did :( ). And certain functions are different: For exmple, it’s: RAND() in MySQL but RANDOM() in SQLite.

    Just saying this because I spent a lot of time figuring out why my tests aren’t working as expected (development, production: MySQL, test: SQLite)

    Well, anyway: I like how easy it is to set up a new project using SQLite.

  • cakebaker March 15, 2009 at 18:39

    @Andreas: Thanks for your comment!

    Yes, such little incompatibilities between the databases can be annoying. Hence I prefer to use the same database type in all environments, as it eliminates a possible source of problems.

    And yes, setting up a new project is quite smooth, even with MySQL (as soon as you learned about the “-d” option *g*).

  • rbn March 22, 2009 at 20:56

    Hi!
    just wandering…
    does anybody know how do you config rails to make mysql and not sqlite the default adapter?
    thnx

  • cakebaker March 23, 2009 at 18:03

    @rbn: You can define the variable RAILS_DEFAULT_DATABASE and set it to mysql, see the hint I added to the article.

    Hope that helps!

  • rbn March 23, 2009 at 19:01

    fantastic… :)) thank you very much for your kind and quick response!

  • cakebaker March 25, 2009 at 16:58

    @rbn: You are welcome!

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License