2012-06-23 30 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