Configuring the database for a Rails application is frighteningly easy — all of the critical information is contained in just one file. We’ll take a look at it now, then create some databases for our application to use.
The Database Configuration File
The separation of environments is reflected in the Rails database configuration file database.yml. We saw a sample of this file back in Chapter 1, Introducing Ruby on Rails, and in fact we created our very own configuration file in Chapter 2, Getting Started, when we used the
rails command. Go take a look! It lives in the
config subdirectory of our Shovell application.
With the comments removed, the file should look like this (Depending on your MySQL configuration, you may need to use
127.0.0.1 as your host value, instead of
Example 4.1. 01-database.yml development: adapter: mysql database: shovell_development username: root password: host: localhost test: adapter: mysql database: shovell_test username: root password: host: localhost production: adapter: mysql database: shovell_production username: root password: host: localhost
This file lists the minimum amount of information we need in order to connect to the database server for each of our environments (development, test, and production). With the default setup of MySQL that we installed in Chapter 2, Getting Started, we can be confident to proceed with our development using the
root user and an empty password for the time being — all of our development should take place on a local machine, so we needn’t be concerned about someone accessing our super-secret Shovell application.
The parameter database sets the name of the database that’s to be used in each environment. As the configuration file suggests, Rails is able to support multiple databases in parallel. Note that we’re actually talking about different databases here, not just different tables — each database can host an arbitrary number of different tables in parallel. Figure 4.1 shows a graphical representation of this architecture.
Figure 4.1. The database architecture of a Rails application
However, there’s one vital aspect missing from our current configuration: the databases referenced in our configuration file don’t exist yet! Let’s create them now.
We can create these databases using one of the many graphical front ends that are available for MySQL, or we can just jump into the command line. Because the commands are fairly simple, let’s create the databases from the command line for now; we’ll look at graphical database clients later in this chapter.
Creating the Databases
To launch the MySQL command line interface, type
mysql -u root at the command prompt. (On a Mac, the command is called
mysql5 instead of
mysql — Mac users like to be different.)
$ mysql -u root mysql>
The command to create a new database is simple enough:
create database newdatabasename.
We’ll use it to create three databases — one for each of our environments — as shown in Figure 4.2.
Example 4.2. 02-create-databases.sql CREATE DATABASE shovell_development; CREATE DATABASE shovell_test; CREATE DATABASE shovell_production;
Database Security and the
If you have any experience with databases, you might be feeling a little uncomfortable that we’re developing our application using the root user, without even setting a password. The reasoning behind this advice is as follows:
1. The default configuration of MySQL is such that connections to the database server can only be made from the same machine. This means that nobody — whether they’re sitting next to you, or working from the other side of the world — will be able to wreak havoc in your Rails development environment.
2. The MySQL command line and permissions system are complex and powerful tools, and database security is a topic that’s definitely beyond the scope of this book.
Of course, this is not a configuration that I would recommend for your production environment, but we’ll get into that in Chapter 12, Deployment and Production Use. If you’re interested in securing the database in your development environment, the MySQL manual contains some post-installation instructions that should serve you well.
Figure 4.2. Creating a database for each environment
Now that our databases exist, we can use them to store data for our application!
development is the Default Database
By default, all Rails applications use the development environment unless specified otherwise. So any Rails commands that you execute from the command line will, for the time being, only affect the data in the development database. In Chapter 12, Deployment and Production Use, we’ll learn how to switch to the production environment.
Patrick has been developing web applications for ten years. Founder and lead developer of the freshmeat.net software portal, he and his Rails consultancy and application development company, limited overload were responsible for a major relaunch of Germany's biggest infotainment community. Patrick lives in Wiesbaden, Germany. His weblog can be found at poocs.net.