MongoDB MongoMapper Ruby Replica Set Config

I am using this configuration to connect to MongoDB with MongoMapper in my Sinatra application:

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

I now have a replica set with 2 mongos on separate servers, 10.5.5.5 and 10.5.5.6. How to set up a connection with both mongors? How do I add authentication to this connection?

+3


source to share


2 answers


I ended up doing this:

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")



Works great.

+1


source


You can set a different connection for each model . But I guess this is not exactly what you are trying to do.

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

      

Or there is a ReplSetConnection with this you can set your replication sets:



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

      

And authentication is simple:

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

      

+1


source







All Articles