Ruby on Rails
MySQL
SQLite
Web Development
Database Configuration

Create a new Ruby on Rails application using MySQL instead of SQLite

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Rails defaults to SQLite for quick local setup, but many teams want MySQL from day one to match production behavior and avoid migration surprises later. Creating a new Rails app with MySQL is easy if MySQL client libraries and adapter gems are installed correctly. Most failures are environment-related: missing native dependencies, bad credentials, or incorrect socket/host settings.

This article shows a clean MySQL-first Rails setup and common troubleshooting steps.

Core Sections

1. Generate app with MySQL adapter

bash
rails new myapp -d mysql
cd myapp

Rails configures config/database.yml for MySQL automatically.

2. Install required MySQL client dependencies

Example macOS:

bash
brew install mysql
bundle install

Example Ubuntu:

bash
sudo apt-get install -y mysql-server libmysqlclient-dev
bundle install

Native adapter (mysql2) depends on system headers/libs.

3. Configure database.yml

yaml
1default: &default
2  adapter: mysql2
3  encoding: utf8mb4
4  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
5  username: root
6  password: <%= ENV["MYSQL_PASSWORD"] %>
7  host: 127.0.0.1

Use environment variables for secrets, not hardcoded passwords.

4. Create and migrate database

bash
bin/rails db:create
bin/rails db:migrate

If create fails, verify MySQL service status and credentials.

5. Run app and verify connection

bash
bin/rails server

Check logs for successful ActiveRecord connection initialization.

6. Dockerized development option

yaml
1services:
2  db:
3    image: mysql:8
4    environment:
5      MYSQL_ROOT_PASSWORD: secret
6      MYSQL_DATABASE: myapp_development

Containerized DB reduces local machine dependency drift.

Common Pitfalls

  • Generating app with default SQLite then partially switching config later.
  • Missing system packages required to compile/install mysql2 gem.
  • Using wrong host/socket settings for local MySQL installation mode.
  • Committing plaintext DB passwords in repository configs.
  • Ignoring charset/collation defaults that differ from production expectations.

Summary

To create a Rails app with MySQL, generate with -d mysql, ensure system MySQL dependencies are installed, configure credentials safely, and run db:create/db:migrate. Align development and production database behavior early to avoid environment-specific bugs later. With proper adapter setup and credential hygiene, MySQL-first Rails workflows are straightforward and stable.

A practical way to make this topic robust in real systems is to define behavior contracts explicitly and test them at boundaries, not only in happy-path unit tests. For create a new ruby on rails application using mysql instead of sqlite, start by documenting the accepted input forms, normalization rules, and expected outputs in edge conditions such as null values, empty collections, malformed payloads, and partial failures. Then add representative fixtures from production logs so tests reflect the real data shape rather than idealized samples. This approach catches compatibility problems early when dependencies, framework versions, or infrastructure defaults change. It also improves onboarding because new contributors can understand the rules without reverse-engineering implicit behavior from scattered call sites.

Operationally, pair implementation changes with lightweight observability so regressions are visible before they become incidents. Emit structured diagnostics around decision points with stable field names for version, environment, execution path, and outcome. Keep sensitive values redacted, but preserve enough context to trace failures quickly. During post-incident reviews, convert each root cause into a permanent regression test and a short runbook update. Over time this creates compounding reliability: fewer repeated bugs, faster triage, and safer refactoring. For teams maintaining create a new ruby on rails application using mysql instead of sqlite across multiple services, centralizing shared helper logic and validating compatibility in CI before rollout usually delivers the biggest reduction in operational noise.

As a final engineering practice, keep one small benchmark or smoke test dedicated to this topic and run it in CI on dependency updates. That single guard often catches behavior drift before users notice it, and it gives maintainers a fast signal when a framework upgrade changes defaults or execution semantics.


Course illustration
Course illustration

All Rights Reserved.