MongoDB won't connect to MongoHQ using Mongoid

拜拜、爱过 提交于 2019-12-20 02:32:36

问题


I've just started a brand new rails project and the first task I'm trying to complete is creating an object and having it save within my database. I went through the automated way of generating an object, in this case a URL object as follows:

rails generate scaffold Url domain:string

I've also tested out two separate mongoid.yml configuration settings. The first is as follows:

development:
  sessions:
    default:
      # Defines the name of the default database that Mongoid can connect to.
      # (required).
      uri: mongodb://[username]:[password]@flame.mongohq.com:27046/[database]

      options:
        consistency: :strong 

The result of this configuration is that when a query takes place, I get the following error:

The operation: #<Moped::Protocol::Query
  @length=39
  @request_id=5
  @response_to=0
  @op_code=2004
  @flags=[]
  @full_collection_name=".urls"
  @skip=0
  @limit=0
  @selector={}
  @fields=nil>
 failed with error 13075: "db name can't be empty"

I also tested with the following configuration:

development:
  uri: mongodb://[username]:[password]@flame.mongohq.com:27046/[database]

When the code gets to the create method of the urls_controller it fails at @url.save with the following error:

mongoid (3.0.0.rc) lib/mongoid/sessions/factory.rb:100:in `parse'
mongoid (3.0.0.rc) lib/mongoid/sessions/factory.rb:61:in `create_session'
mongoid (3.0.0.rc) lib/mongoid/sessions/factory.rb:43:in `default'
mongoid (3.0.0.rc) lib/mongoid/sessions.rb:109:in `default'
mongoid (3.0.0.rc) lib/mongoid/sessions.rb:354:in `__session__'
mongoid (3.0.0.rc) lib/mongoid/sessions.rb:199:in `mongo_session'
mongoid (3.0.0.rc) lib/mongoid/sessions.rb:157:in `collection'
mongoid (3.0.0.rc) lib/mongoid/sessions.rb:25:in `collection'
mongoid (3.0.0.rc) lib/mongoid/persistence/operations.rb:26:in `collection'
mongoid (3.0.0.rc) lib/mongoid/persistence/operations/insert.rb:27:in `block in persist'
mongoid (3.0.0.rc) lib/mongoid/persistence/insertion.rb:25:in `block (2 levels) in  prepare'
activesupport (3.2.3) lib/active_support/callbacks.rb:403:in `_run__2492706777632263523__create__3276423133299307975__callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.3) lib/active_support/callbacks.rb:385:in `_run_create_callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
mongoid (3.0.0.rc) lib/mongoid/callbacks.rb:95:in `run_callbacks'
mongoid (3.0.0.rc) lib/mongoid/persistence/insertion.rb:24:in `block in prepare'
activesupport (3.2.3) lib/active_support/callbacks.rb:403:in  `_run__2492706777632263523__save__3276423133299307975__callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.3) lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
mongoid (3.0.0.rc) lib/mongoid/callbacks.rb:95:in `run_callbacks'
mongoid (3.0.0.rc) lib/mongoid/persistence/insertion.rb:23:in `prepare'
mongoid (3.0.0.rc) lib/mongoid/persistence/operations/insert.rb:26:in `persist'
mongoid (3.0.0.rc) lib/mongoid/persistence.rb:49:in `insert'
mongoid (3.0.0.rc) lib/mongoid/persistence.rb:188:in `upsert'
app/controllers/urls_controller.rb:46:in `block in create'

My assumption is that my mongoid.yml file is incorrect but I'm not sure how to get this to work. Any thoughts?


回答1:


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




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/10801977/mongodb-wont-connect-to-mongohq-using-mongoid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!