Ruby on Rails App w/ MySQL on macOS Catalina v10.15

Osha Groetz
3 min readOct 23, 2020

The default database for Ruby on Rails is SQLite. SQLite, although sufficient for beginner apps, is not the best choice for production of more large scale applications, mostly due to it not allowing more than one user to write to the database at a time (concurrency). Having learned how to instead connect a PostgreSQL database through Flatiron, I thought I’d take the opportunity to learn and install MySQL. This post will walk you through installation of MySQL, MySQL Workbench (for viewing your database), and the first steps of building a Rails app with MySQL as your db.

A gif to keep you reading on…

Installing MySQL:

Visit the MySQL website here: https://dev.mysql.com/downloads/mysql/. Download the first file ‘macOS 10.15 (x86, 64-bit), DMG Archive‘ as of 10/2020 the version is 8.0.22 . Clicking download will immediately bring you to a a page asking you to log in or sign up for ‘Oracle’. Skip this by selecting the link below the large prompt buttons ‘No thanks, just start my download’. After the download is complete, launch the .dmg file and open the .pkg file and choose the ‘continue’ prompt in the window it opens for installation. When the installation is complete, the window will prompt you to either “Use Strong Password Encryption” or “Use Legacy Password Encryption”. Choose the first option of “Use Strong Password Encryption”, “Next”, and then choose a “root” password. REMEMBER THIS PASSWORD, you’ll need it for all of your apps that use MySQL. Select ‘Finish’. Open ‘System Preferences’. At the bottom of this window, you should see the MySQL icon. Open MySQL and you should see that the server is already running and our active and installed instances have green buttons to their left.

MySQL in System Preferences

To install MySQL Workbench:

This app does NOT automatically with the MySQL download, you will need to manually download it separately from: https://dev.mysql.com/downloads/workbench/. Currently, the download to chose is the only one available on the page ‘macOS (x86, 64-bit), DMG Archive‘. Follow the same instructions above, skipping any sign in to Oracle, and just beginning the download, open the dmg file, etc. When prompted drag and drop the app into the application file and open the app. On the bottom left hand side of the window upon opening the app, select the box with the header ‘Local instance 3306’. You’ll be prompted to login — you’ll need to use your MySQL password. When saved, the main app will open and here you can create and view databases, tables, columns within tables, etc., and Query these databases.

Starting a new Ruby on Rails application with a MySQL database:

In your terminal, cd to the folder that you’d like this application to live. In the command line run: “rails new <app-name> -d=mysql”. We are going to use this command to immediately create a new app and upon creation set it up with a MySQL database, completely eliminating the creation of the SQLite db altogether. If the app has already been created and you would like to change the db from SQLite to MySQL, there are tutorials that can help you do so. When installation is complete, cd into your app and open it in your text editor. You’ll need to check a few things in your app files to make sure installation is successful:

  1. Gemfile: make sure that you see ‘gem ‘mysql2’’ and NOT ‘gem ‘sqlite3’’
  2. config/database.yml: you’ll make sure your default looks somewhat similar to this code:

default: &default

adapter: mysql2

encoding: utf8mb4

pool: <%= ENV.fetch(“RAILS_MAX_THREADS”) { 5 } %>

username: root

password: <<enter your MySQL password here>>

socket: /tmp/mysql.sock

Next, you’ll open up your terminal within your app root file and run command: “rake db:create”. Your terminal, before giving you the next prompt line, should tell you that production and test databases were created. SUCCESS! You should be able to now create migrations and run “rake db:migrate”, setting up your schema. If you open MySQL Workbench, change to the “schemas” tab, your tables should show in the left hand pane.

--

--

Osha Groetz

Software Engineer. React, Javascript, Typescript, HTML, CSS, Ruby, Ruby On Rails