3

我有兩個數據庫,我必須在我的應用程序中使用。我在我的mongoid.yml如下:連接到兩個數據庫Mongoid

development: 
    # Configure available database sessions. (required) 
    sessions: 
    # Defines the default session. (required) 
    default: 
     # Defines the name of the default database that Mongoid can connect to. 
     # (required). 
     database: db_development 
     username: myusername 
     password: mypassword 
     # Provides the hosts the default session can connect to. Must be an array 
     # of host:port pairs. (required) 
     hosts: 
     - myserverip:27017 
     databases: 
     secondary: 
      database: db2_development 
      username: myusername 
      password: mypassword 
      # Provides the hosts the default session can connect to. Must be an array 
      # of host:port pairs. (required) 
      hosts: 
      - myserverip:27018   

在我的模型文件:

class MyModel 
    include Mongoid::Document 
    store_in database: "secondary" 
    field :name, type: String 
    field :age, type: Integer 
end 

我有數據爲MyModel。當我嘗試查詢時,出現以下錯誤:

Moped::Errors::QueryFailure (The operation: #<Moped::Protocol::Query 
    @length=96 
    @request_id=5 
    @response_to=0 
    @op_code=2004 
    @flags=[:slave_ok] 
    @full_collection_name="secondary.mymodel" 
    @skip=0 
    @limit=0 
    @selector={"name"=>"Tom"} 
    @fields=nil> 
    failed with error 10057: "unauthorized db:secondary ns:secondary.mymodel lock type:0 client:10.100.55.40" 

我試過在線搜索,但無法獲得任何解決方案。任何幫助,將不勝感激。提前致謝。

回答

12

嗯以及可你這樣做看起來像你搞砸你的YAML文件

development: 
    sessions: 
    default: 
     database: db_development 
     username: my_username 
     password: my_password 
     hosts: 
     - myserverip:27017 
     options: 
     consistency: :eventual 
    writeable: 
     database: db2_development 
     username: myusername2 
     password mypassword2 
     hosts: 
     - myserverip2:27018 
     options: 
     consistency: strong 

在模型中只寫這個

store_in session: "writeable"

class MyModel 
    include Mongoid::Document 
    store_in session: "writeable" 
    field :name, type: String 
    field :age, type: Integer 
end 

FYI決不password的選擇,但我測試猜它會工作

希望這個幫助

+0

它的作品就像一個魅力。謝謝。 +1! – senthil 2013-05-07 06:45:01

+0

感謝您的回答 我說的「localhost:27017」,而不是「myserverip:27017」 和「store_in數據庫:‘db2_name’」,而不是「在會話存儲:‘寫’」 和它的工作對我來說 – 2017-09-16 08:47:36