Airbnb Clone in Rails: Switching to MySQL

Published Sep 13, 2017
Airbnb Clone in Rails: Switching to MySQL

In the beginning, we used SQLite to reduce the workload of our databases. We ran into a problem because Cloud9 doesn’t provide SQLite GUI built-in support.

This time, we'll be back in our comfort zone. Fortunately, Rails migration is supported by multiple database types. In this tutorial, we will go through the integration step by step.

Available Ports

The only available ports in Cloud9 are 8080, 8081, and 8082. If we start Apache for phpMyAdmin and start Rails server, it will conflict with the Rails server. To avoid this, follow these steps:

  1. Type sudo nano /etc/apache2/ports.conf

  2. Change port 8080 to 8081

  3. Press Ctrl or Command + X to save and exit

  1. Next, restart Apache with service apache2 restart and phpmyadmin-ctl install
     , then go to that URL and type in the extra port like so:

Install MySQL Gem

  1. Open Gemfileand insert:
gem 'mysql2'
  1. Then command bundle install

Change Database Adapter

  1. Ppen config/database.yml and comment sqlite3 adapter like so:
default: &default # adapter: sqlite3 # pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> # timeout: 5000 adapter: mysql2 encoding: utf8 database: c9 username: krissanawat101 password: "" host: port: 3306
  1. Change database name to c9
development: <<: *default database: c9
  1. Restart Rails server

Migrate to New Database

In the above image, you can see that we received "error" — this is because the type of column is wrong for MySQL. To fix this:

  1. Open /db/migrate/20170904031807_add_confirmable_to_devise.rb and change to datetime:

  1. Drop old table

  1. Migrate again!

Here's what it should look like if it worked out correctly:

If you have any questions, feel free to leave a comment!

Discover and read more posts from Krissanawat Kaewsanmuang
get started
Enjoy this post?

Leave a like and comment for Krissanawat

Be the first to share your opinion

Get curated posts in your inbox

Read more posts to become a better developer