sen*_*hil 6 querying multiple-databases mongoid ruby-on-rails-3 mongoid3
我有两个数据库,我必须在我的应用程序中使用.我的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
Run Code Online (Sandbox Code Playgroud)
在我的模型文件中:
class MyModel
include Mongoid::Document
store_in database: "secondary"
field :name, type: String
field :age, type: Integer
end
Run Code Online (Sandbox Code Playgroud)
我在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"
Run Code Online (Sandbox Code Playgroud)
我尝试在线搜索但无法获得任何解决方案.任何帮助,将不胜感激.提前致谢.
Vir*_*ren 15
嗯,你能做到这一点似乎你搞砸了你的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
Run Code Online (Sandbox Code Playgroud)
在你的模型中写下这个
store_in session: "writeable"
class MyModel
include Mongoid::Document
store_in session: "writeable"
field :name, type: String
field :age, type: Integer
end
Run Code Online (Sandbox Code Playgroud)
FYI从未测试过password选项,但我猜它会起作用
希望这有帮助
| 归档时间: |
|
| 查看次数: |
5082 次 |
| 最近记录: |