2012-06-23 36 views
3

我有一個非常直接的主/從複製設置與MySQL。 ActiveRecord被配置爲按照database.yml中的預期命中主主機名。我的問題是,目前是否有可能爲ActiveRecord設置一個選項來執行從設備的所有讀取操作並將寫入操作留給主設備?我正在使用Ruby on Rails/ActiveRecord 3.1.3。我沒有找到任何選項來執行以下操作,只是database.yml中的一個主機輸入字段。目前這可能嗎?ActiveRecord配置讀取從機MySQL框

回答

4

是的,這是可能的,但你需要使用它的寶石;它不是內置於Rails本身。

我目前使用seamless database pool,它的效果很好。 Octopus是另一種選擇。

2

makara gem。它將所有SELECT語句發送給從站並將操作寫入主站。

添加這就像你Gemfile

gem 'makara' 

,然後配置你的database.ml這樣的:

production: 
    adapter: 'mysql2_makara' 
    database: 'MyAppProduction' 
    # any other standard AR configurations 

    # add a makara subconfig 
    makara: 

    # the following are default values 
    blacklist_duration: 5 
    master_ttl: 5 
    sticky: true 

    # list your connections with the override values (they're merged into the top-level config) 
    # be sure to provide the role if master, role is assumed to be a slave if not provided 
    connections: 
     - role: master 
     host: master.sql.host 
     - role: slave 
     host: slave1.sql.host 
     - role: slave 
     host: slave2.sql.host 
+0

我試過使用Makara,但是有沒有什麼辦法可以使默認讀取是從主機,只有某些查詢是從奴隸?八達通允許使用「:using」子句。 – Yahya

+0

我找到了我的問題的答案。檢查這個鏈接:https://github.com/ankane/shorts/blob/master/Scaling-Reads.md – Yahya