Does Ruby on Rails Work With MySQL?
Ruby on Rails has first-class MySQL support through ActiveRecord and works seamlessly with MySQL as a primary database.
Quick Facts
How Ruby on Rails Works With MySQL
Ruby on Rails integrates with MySQL through the `mysql2` gem, which provides the database adapter for ActiveRecord ORM. When you generate a new Rails application with `rails new myapp --database=mysql`, the framework automatically configures database.yml with MySQL connection settings and installs the necessary dependencies. ActiveRecord abstracts SQL queries into Ruby method calls, so you write `User.where(active: true)` instead of raw SQL, making the developer experience clean and intuitive.
The combination is production-proven and widely used in enterprise applications. MySQL handles Rails migrations seamlessly, allowing you to version control schema changes as Ruby code. Connection pooling, transaction management, and query optimization all work out-of-the-box. Rails' convention-based approach means minimal configuration—just set your database credentials in environment variables and you're ready to go. The ecosystem is mature, with excellent documentation and community support for debugging common issues.
Best Use Cases
Quick Setup
rails new myapp --database=mysql# config/database.yml is auto-generated
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: <%= ENV['DB_PASSWORD'] %>
host: localhost
development:
<<: *default
database: myapp_development
# Generate and run migrations
rails generate migration CreateUsers
rails db:migrate
# Example model with MySQL-specific features
class User < ApplicationRecord
validates :email, presence: true, uniqueness: true
endKnown Issues & Gotchas
MySQL strict mode compatibility with Rails versions below 5.1
Fix: Upgrade to Rails 5.1+ or disable strict mode in MySQL my.cnf. Modern Rails handles strict mode correctly.
Character encoding issues when migrating legacy MySQL databases
Fix: Explicitly set charset and collation in migrations: `create_table :users, charset: 'utf8mb4', collation: 'utf8mb4_unicode_ci'`
N+1 query problems when eager loading associations
Fix: Use `.includes()`, `.preload()`, or `.eager_load()` in ActiveRecord queries to batch load related records
MySQL 5.7+ requires generated columns definition for some Rails features
Fix: Upgrade to MySQL 8.0 or use explicit column definitions in migrations for compatibility
Alternatives
- •Rails with PostgreSQL - more advanced features like JSONB and full-text search
- •Rails with SQLite - lightweight for prototyping and small projects
- •Django with MySQL - Python alternative with similar ORM abstraction
Resources
Related Compatibility Guides
Explore more compatibility guides