MongoDB won't connect to MongoHQ using Mongoid

旧城冷巷雨未停 提交于 2019-12-01 21:43:34

This is how my production mongoid.yml looks like

<% if ENV['MONGOLAB_URI'] %>
  <% uri = URI.parse(ENV['MONGOLAB_URI']) %>
production:
  <<: *defaults
  sessions:
    default:
      <<: *default_session
      database: <%= uri.path.sub('/','') %>
      username: <%= uri.user %>
      password: <%= uri.password %>
      hosts:
        - <%= uri.host %>:<%= uri.port %>
<% end %>

and this works for me

Database connections currently cannot be made via URI at this point in mongoid. The format of the mongoid.yml is as follows:

environment:
  host: <%= ENV['MONGOID_HOST'] %>
  port: <%= ENV['MONGOID_PORT'] %>
  username: <%= ENV['MONGOID_USERNAME'] %>
  password: <%= ENV['MONGOID_PASSWORD'] %>
  database: <%= ENV['MONGOID_DATABASE'] %>

What you will need to do is parse the MongoHQ string into its constituent parts to supply in the mongoid.yml

If you want, someone has cooked up a gist that does this for you here

For a replica set / non-replica-set on MongoHQ:

production:
  sessions:
    default:
      <% if ENV['MONGOHQ_HOST_LIST'] %>
      database: <%= ENV['MONGOHQ_DATABASE'] %>
      username: <%= ENV['MONGOHQ_USER'] %>
      password: <%= ENV['MONGOHQ_PASSWD'] %>
      hosts:
      <% YAML.parse(ENV['MONGOHQ_HOST_LIST']).value.each do |v| %>
        - <%= "#{v[0].value}:#{v[1].value}" %>
      <% end %>
      <% elsif ENV['MONGOHQ_URL'] %>
      uri: <%= ENV['MONGOHQ_URL'] %>
      <% end %>
      options:
        consistency: :eventual
        safe: true
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!