MongoDB MongoMapper Ruby副本集配置

时间:2015-01-13 03:25:31

标签: ruby mongodb sinatra

我正在使用此配置在我的Sinatra应用程序中使用MongoMapper连接到MongoDB:

MongoMapper.connection = Mongo::Connection.new('localhost', 27017)

现在我在不同的服务器上拥有2个mongos的副本集,10.5.5.5和10.5.5.6。如何设置与两个mongos的连接?如何向此连接添加身份验证?

2 个答案:

答案 0 :(得分:1)

您应该可以设置不同的连接per model。但我想这并不是你想要做的。

class MyModel
  include MongoMapper::Document
  connection(Mongo::Connection.new('localhost', 27017))
  set_database_name "my_database"
  # ...
end

或者ReplSetConnection可以设置您的复制集:

MongoMapper.connection = Mongo::ReplicaSetConnection.new(['10.5.5.5', 30000], [' 10.5.5.6', 30000])

身份验证很简单:

MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "DBNAME"
MongoMapper.database.authenticate("USERNAME", "PASSWORD") 

答案 1 :(得分:1)

我最终这样做了:

MongoMapper.connection = Mongo::MongoReplicaSetClient.new( ['10.5.5.5:27017', '10.5.5.6:27017'], :read => :primary, :rs_name => 'name', :connect_timeout => 30, :op_timeout => 30 ) MongoMapper.database = "db_name" MongoMapper.database.authenticate("user", "test123")

工作得很漂亮。