Connecting to two databases Mongoid

心已入冬 提交于 2019-12-03 18:53:14

问题


I have two databases that I have to use in my application. I have the following in my mongoid.yml:

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          

In my model file:

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

I have data in MyModel. When I tried to query, I'm go the following error:

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"

I tried searching online but could not get any solution. Any help would be appreciated. Thanks in advance.


回答1:


hmm well can you do this seem like you messed your yaml file

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

In your model just write this

store_in session: "writeable"

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

FYI Never tested with password options but i guess it would work

Hope this help




回答2:


In order to access a database temporarily (e.g. in a script) you can use the MongoDB Ruby drivers: Tutorials - Documentation - Low Level Documentation

For a quick overview:

Connect to the database:

client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'my_db')

db = client.database

Query for entries via mongoDB query syntax:

db['collection_name'].find('field_name' => 'field_value')



来源:https://stackoverflow.com/questions/16400458/connecting-to-two-databases-mongoid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!