连接到两个数据库Mongoid [英] Connecting to two databases Mongoid
本文介绍了连接到两个数据库Mongoid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个必须在应用程序中使用的数据库.我的 mongoid.yml 中有以下内容:
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
在我的模型文件中:
class MyModel
include Mongoid::Document
store_in database: "secondary"
field :name, type: String
field :age, type: Integer
end
我的MyModel中有数据.当我尝试查询时,出现以下错误:
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.
推荐答案
嗯,您能做到吗,好像您弄乱了Yaml文件
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
在您的模型中只需编写
store_in session: "writeable"
class MyModel
include Mongoid::Document
store_in session: "writeable"
field :name, type: String
field :age, type: Integer
end
仅供参考,我从未使用password
选项进行过测试,但我认为它会起作用
FYI Never tested with password
options but i guess it would work
希望获得帮助
这篇关于连接到两个数据库Mongoid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文