mongoid 连接到别的服务器数据库

1、在config 文件下面新件一个文件mongoid.yml,里面的文件格式 :

defaults: &defaults
  host: 服务器ip地址
  # slaves:
  #   - host: slave1.local
  #     port: 27018
  #   - host: slave2.local
  #     port: 27019

development:
  <<: *defaults
  database: database-name
 # databases:
  #  brand:
   #   database: database-name
    #  host: 服务器ip地址
    #  port: 27107
test:
  <<: *defaults
  database: database-name

# set these environment variables on your prod server
production:
  host: 服务器ip地址
  port: 27017
  database: database-name

2、在initializers新建一个mongoid_conf.rb

mongoid_conf = YAML::load_file(Rails.root.join('config/mongoid.yml'))[Rails.env]

Mongoid.configure do |config|
# puts mongoid_conf['host']
# puts mongoid_conf['port']
# puts mongoid_conf['database']
 config.master = Mongo::Connection.new(mongoid_conf['host'],
                                       mongoid_conf['port'],:logger => Rails.logger).db(mongoid_conf['database'])
end

你可能感兴趣的:(mongoid)