LoginSignup
246
232

More than 5 years have passed since last update.

railsのDBをmysqlに変更する。

Last updated at Posted at 2013-03-06

最初に、

$ rails new アプリケーション名 -d mysql
or
$ rails new アプリケーション名 --database=mysql

でやれば、出来るのだが、今回は後から変更することを想定しています。

database.yml
development:
  adapter: mysql2
  encoding: utf8
  database: blog_development
  pool: 5
  username: root
  password:
  socket: /tmp/mysql.sock

に設定を変更。
この内容は、 $ rails new アプリケーション名 -d mysql でアプリケーションを作成した時のconfig/database.yml を参考に記述しているだけです。

DB設定を更新したので、DB側の準備をしていきます。

bundle exec rake db:create

を行うと、以下のエラーが出ました。

(in [DIRECTORY])
rake aborted!
Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (mysql2 is not part of the bundle. Add it to Gemfile.)

内容は、mysql用のアダプタがないということです。
したがってGemfileを書き換えて、mysqlのアダプタのgemを追加していきます。

# gem 'sqlite3' <- 必要ないので削除
gem 'mysql2'

コンソールに移って、

$ bundle install

これで、mysqlのアダプタのgemを加える事ができました。
更に、mysqlにDBの追加と、テーブル作成を行っていきます。

$ bundle exec rake db:create
$ bundle exec rake db:migrate
==  CreateTables: migrating ===================================================
-- create_table(:tables)
   -> 0.0186s
==  CreateTables: migrated (0.0187s) ==========================================

これで完了です。

246
232
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
246
232