Learn to create a new database from the command line using Rails instead of having to log into MySQL to create it.
- [Instructor] In this chapter, we will take a deeper dive into Active Record database migrations so that we can learn about some useful features and time saving shortcuts. Let's begin by learning how to use Rails to create a database. What we want to do is be able to create a database from the command line using the Rails command. That is, instead of going into MySQL and creating the database. If you took my Ruby on Rails Essential Training, that's the way we did it the first time. We logged into MySQL, we created the database, and then we created a MySQL user and we gave it permissions for that database, and then we created our Rails app and configured everything to work with it.
Here we want to do it a little differently. We want Rails to actually do the MySQL command to create the database. To do that, we still need to have a MySQL user. That could just be your root user, if you wanted to use your root user to log in and do the command. Or it could be another user, provided that that user has privileges to create new databases. Once we have a MySQL user that our Rails app can use to log in, then we want to configure the database.yml file with the information about how to log in and what database it should create when it gets there.
After we do that, it's just a simple matter of typing rails db:create from the command line and it will create the databases for us. So now let's jump back to that second point, create MySQL user. In order to create MySQL user, you would need to go into MySQL as your root user and then create a new user and grant them privileges. And you can do that with this command, grant all privileges on *.* to, and then the name of the user you want to use, @localhost.
Localhost just means that the user can only connect from this local machine. They cannot connect from a remote IP address somewhere else. And then, identified by, and then whatever password you want to use for them. I've got something simple here. You can pick something much better than that, I'm sure. Now let's go back to that *.* part. That's important. What it's saying is that this Rails user that we're creating and giving a password to, they have all privileges on all databases and all tables. That first asterisk is important. They don't have access to just one database, they have access to all databases.
And that means that they're able to create new databases as well. So you want to type it exactly like this with the semi-colon at the end. Once you have that, you'll have a user that's able to create new databases. Now, for this, I'm going to be using a new Ruby on Rails project. Brand new one. And the project I'm going to be using is also in the exercise files, if you want to get it there. But you can also just create a brand new Rails app easily for yourself with rails new demo_project, and then specifying the database as -d mysql. You can see that in my command line, that I've navigated into that project that I've created.
So that's where I'm located right now. And I've also got that opened up in my Atom text editor. And I've gone ahead and opened up config database.yml. I've also got another window opened up over here where I've logged into MySQL directly. And this is just so we can actually see the databases and know whether they've been created or not. So I've logged in as root and I've just put in a command here, show databases like, and then I specified the name of the database that we're going to be creating. You can just do show databases as well, you'll just see a bunch of extra databases in addition to this one.
This will just show us only ones that match this pattern, demo_project_, and then we've got a wildcard there that's the percent sign. The databases we're going to create are going to be demo project development and demo project test. By default, Rails is going to create both of these for us. It used to only create the development one, but it goes ahead and now helpfully creates the project test at the exact same time. Notice that both of these inherit from this default configuration that we have up here. There's also a listing for production down here, which has that default configuration, but then overrides some of those by having the user name and the password be specific to production.
So you can override those values easily. Now when we create the database, it's not going to create the production database, it's not going to create anything related to default. It's only going to look for development and test. Let's configure our file now. The user name that it's going to use right now is root. And as I said at the beginning, you can log in as root and create your database that way. It would work. But I'm going to go ahead and use this Rails user that we created and use it's password, secretpassword. And that will allow me to log in as the Rails user and create the database that way.
It's really up to you. I just have a preference to not use the root user whenever possible. Alright, so once I got those both in there, let's save the file. So now we've created the first two steps. We have our MySQL user and we've configured our database.yml file. Now we just need to go over here to our project, and let's run that command, rails db:create. Hit return. Wait a moment. It reads our database file. It logs into MySQL. And it executes the command to create those two databases for us, the development database and the test database.
If we come back over to MySQL, I'm going to hit the up arrow and we'll run that same command again and now you'll see that those new two databases did appear. They both now exist. Now we walked thorugh those steps slowly. But if you had had this Rails user existing, you could've just very easily have created your project, come into here, type the user name, type the password, switch back over to the command line, typed rails db create, and you'd already have your databases ready to start working. It can save you time and prevent you from making an extra trip to log into MySQL.
- Migrating databases
- Creating, updating, and deleting records
- Tracking changes to objects and attributes
- Writing queries
- Delegating attributes
- Working with associations