Skip to content

Convert Rails SQLite to MySQL

Convert Rails SQLite to MySQL#

Add the MySQL Adapter#

Replace sqlite3 with mysql2 in the Gemfile:

gem "mysql2"

Then install:

bundle install

Update config/database.yml#

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV.fetch("MYSQL_USER") %>
  password: <%= ENV.fetch("MYSQL_PASSWORD") %>
  host: <%= ENV.fetch("MYSQL_HOST") { "localhost" } %>

development:
  <<: *default
  database: myapp_development

If you use a Unix socket locally, check the socket path:

mysqladmin -u root -p variables | grep socket

Create the Database#

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

Sources#