TAGS:

ruby  rails  mysql  database 

1- Installing Mysql-server and Creating Database.

sudo apt-get install mysql-server

Then log into mysql-server with root account:

mysql -u root -p

Type root password and be ready to use mysql DSL. First, you may need to determine that you will create more than one database, one for development environment, and one for test, and the third for production environment.

Here, We will create one for development only, and you can create the others.

create database rails_dev;

You can see all mysql databases on system using:

show databases;

Also, You need to create user rather than root:

create user 'osama'@'localhost' identified by '123456789';

Then, Grant all privileges to this user

grant all privileges on * . * to 'osama'@'localhost';

At this point, you have database name, username and password.



2- Creating Rails Application With Mysql database.

rails new auto --database=mysql
cd auto
subl .

The default database for rails is sqlite3, but we use --database=mysql to use mysql instead, so you will find ` gem ‘mysql2’.

Note, If you change from sqlite3 to mysql, you need to make

bundle install

to update gemfile.lock and install mysql gem if not installed.

Open config/database.yml and fill

  default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: osama       #of mysql database created.
  password: 123456789   #of mysql database created.
  socket: /var/run/mysqld/mysqld.sock

development:
  <<: *default
  database: rails_dev   #of mysql database created.

Now, Create you posts using scaffold :

rails g scaffold post name comment:text

And then, Make migration to create the posts table in the database it self.

rake db:migrate

Last, Fire up you rails server:

rails s

Now, ready to browse to localhost:3000/posts and process all CRUD operations.

All these steps in this tutorial video :