In this second part of a four-part article series on building a basic Ruby-on-Rails application, you will learn how to create both a database and the event model. This article is excerpted from chapter three of the book Beginning Rails: From Novice to Professional, written by Jeffery Allan Hardy, Cloves Carneiro Jr. and Hampton Catlin (Apress; ISBN: 1590596862).
Database for a Ruby-on-Rails Application (Page 1 of 2 )
Creating the Project Databases
To create a new database, you can use your favorite database administration tool, or you can use the mysqladminprogram from the command line. Since we’re command-line junkies, we’ll do the latter.
$ mysqladmin -uroot create events_development
We give themysqladmin program two arguments: an action (create) and the name of the database we want to create. That should be sufficient to create a new database calledevents_development.
Note Depending on how your environment is set up, you might not need to specify the username under You might also need to supply a password, which you can do using the-poption. Here, we’re using MySQL’s defaultrootuser (with no password), which should work on most installations.
While we’re only concerned with the development environment at this time, it won’t hurt to create the other databases while we’re at it. Go ahead and create two more databases, one each for the test and production environments.
For most MySQL installations, the default connection parameters indatabase.yml(a username ofroot and no password) will work out of the box. If you’ve set up a password for the root account, or if you prefer not to use the root account at all and instead use a different username, you’ll need to fill in that information if you want your connection to work.
You can test to see if your connection is working by running the following command.
$ rake db:migrate
If you see nothing exceptional returned, congratulations! Rails can connect to your database. However, if you see something like this:
-------------------------------------------- rake aborted! Access denied for user 'root'@'localhost' (using password: NO) --------------------------------------------
then you need to adjust your connection settings. If you’re having problems, make sure that the database exists and that you’ve entered the correct username and password.