LoginSignup
0
0

More than 1 year has passed since last update.

【Ruby on Rails】 sqlite3 から mysql に切り替える

Posted at

はじめに

Ruby on Rails で デフォルトのデータベースが sqlite3 になっていますが、mysql に切り替えたいと考え、備忘録としてアウトプットします。

サービス環境

  • ruby 3.0.0
  • Rails 6.0.4
  • docker
  • mysql 8.0.2
  • Slim, SCSS

方法

1. Gemfileの編集

# gem 'sqlite3', '~> 1.4'
gem 'mysql2'

デフォルトの sqlite3 の gem はコメントアウトし、mysql2 を追記します。

2. bundle install

3. コンテナの再度立ち上げ

4. config/database.yml編集

default: &default
  # adapter: sqlite3
  # pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  # timeout: 5000
  adapter: mysql2
  encoding: utf8
  reconnect: false
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: ユーザー名
  password: パスワード
  host: db


development:
  <<: *default
  database: アプリ名_development

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: アプリ名_test

production:
  <<: *default
  database: アプリ名_production

username, password, host, アプリ名に関しては各々のものを設定しましょう!sqlite3 の部分はコメントアウトまたは削除します。

5. db:create

$ docker-compose run web rails db:create

6. db:migrate

$ docker-compose run web rails db:migrate

最後に

$ docker-compose up (-d)

localhost:3000で立ち上げられるかどうか確認してみましょう!

参考記事

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0