8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Rails4]multi_dbを使ってみた

Posted at

DBが最近ボトルネックになっているので、AWSのReadReplicaを使ってみようと思います。

環境

AWS RDS x2.large (MySQL5.6)
Rails 4.0.0

参考

Amazon RDSのリードレプリカ+Rails4.0+multi_dbを試してみました
http://d.hatena.ne.jp/ogin_s57/20130928/1380331765
ご本家
https://github.com/schoefmax/multi_db

実践

  • RDSの『Instance Action』から[Create Read Replica]をポチる。名称を入れてcreateするだけ!素晴らしい。
  • Rails側にmulti_dbを導入する。Gemfileに以下を追加。
    observerを使ってるみたいだけど、rails4だと廃止されてるので、一緒に入れる必要があるみたいです。
gem 'multi_db', '~> 0.3.1'
gem "rails-observers"

追記したらbundle install

  • データベース定義に以下を追加
    ※環境_slave_databse: で定義を記述する。ここではdev環境
config/database.yml
development_slave_database:
  adapter: mysql2
  database: multi_db_development_read
  host: yyyyyyyy.ap-northeast-1.rds.amazonaws.com
  port: 3306
  username: root
  password: replica_password
  • multi_dbを利用するためのコードを追記
config/environments/.rb
  config.after_initialize do
    MultiDb::ConnectionProxy.setup!
  end

わりとあっさり出来ました。
後はコネクション数とか確認しながら、ReadReplicaの台数増やすとかかなぁ。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?