Last Updated: February 25, 2016
·
952
· verkalets

Rails+ Mysql Setup

Hi guys, I want to show you how to use MySQL Database with Rails framework.

For the beginning, lets install MySQL Server on your local machine. For this, go here: http://dev.mysql.com/downloads/mysql/ and select your server version. I’ve choosed version for Mac OS.
After downloading, install server. For this, just open your .dmg file, and proceed install with step by step guide.

To manage MySQL, I’m using MySQL Workbench. You also can download it here: http://dev.mysql.com/downloads/workbench/

Now, when we have installed software, lets try to connect our Rails app and MySQL.

Start our MySQL server:

mysql.server start

Create Rails app:

rails new coderwall-mysql -d mysql

As you can see, rails generate database.yml for MySQL, and added mysql2 gem into gem file.

The next step, what we will make, is to create schemas. Rails does it for you with a simple step:

rake db:create

It’s clear that rails create database for the project automatically. Lets create simple scaffold, for testing.

rails g scaffold Post title:string rating:integer

and migrate:

rake db:migrate

Now, if you open http://localhost:3000/posts, you will see list of post.

Try to add some post to check if it’s correct.

You can see example for this app here: https://github.com/Verkalets/coderwall-mysql