9
7

More than 5 years have passed since last update.

railsでのmysqlの導入の手順 

Last updated at Posted at 2016-03-03

rails 4.2です。
環境はvagrant。

-d mysqlをつけてアプリを作成

$ bundle exec rails new アプリ名 --skip-bundle -d mysql

これで、gemfileを見てみると、デフォルトだとgemがsqliteのところが、mysqlに変更されている。

config/database.ymlここもちゃんとデフォルトのsqliteではなく、mysqlに変更されている。

--skip-bundleしているのでgemはまだインストールされていないからする。

けどその前に、

javascriptのruntimeというのがないとbundle exec rake db:createする時にエラーがでるのでそのtherubyracerというgemを加える。
therubyracerはgemfileにもともとあってコメントアウトされているので、コメントアウトをはずすだけで良い。

gemfile

# See https://github.com/rails/execjs#readme for more supported runtimes

# gem 'therubyracer', platforms: :ruby

gemfile

# See https://github.com/rails/execjs#readme for more supported runtimes

 gem 'therubyracer', platforms: :ruby


$ bundle install

データベースをcreateする

$ rake db:create

これで、もし、

Specified 'mysql2' for database adapter, but the gem is not loaded. Addgem 'mysql2'to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).

というエラーがでたら、

$ bundle updateして、

$ bundle install

これで、$ bundle exec rake db:create

9
7
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
9
7