1

我已經從Mongo_Mapper切換到Mongoid,並且出於某種原因無法部署到生產環境。我正在使用NGINX,Rails 3.1和Passenger。我不斷收到此消息,「無法連接到myusernamehere:27017(Mongo :: ConnectionFailure)上的主節點」。Mongoid生產問題:無法連接到主節點

defaults: &defaults 
    host: localhost 
    # slaves: 
    # - host: slave1.local 
    #  port: 27018 
    # - host: slave2.local 
    #  port: 27019 

    development: 
    <<: *defaults 
    database: s3uploadergen_development 

    test: 
    <<: *defaults 
    database: s3uploadergen_test 

    production: 
    host: localhost 
    port: 27017 
    database: mydbnamehere 
    username: myuserhere 
    password: mypasswordhere 

我有三重檢查所有的設置,並試圖ENV方法,以及(添加ENV變量production.rb並通過記錄mongoid方法調用他們,但有同樣的問題):

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

理想情況下,我想只是在production.rb或某種初始化器中指定它。

+0

我假設通過「記錄的mongoid方法」你的意思是設置推薦的「uri」參數,而不是所有這些不同的設置? – typeoneerror

+0

不,我認爲這隻適用於Heroku,我不使用。你能發佈一個鏈接嗎?或者更好的例子是? – jschorr

回答

1

我假設通過「記錄的mongoid方法」你的意思是設置推薦的「uri」參數,而不是所有這些不同的設置。您可能想嘗試一下,因爲這是推薦的方式。

defaults: &defaults 
    persist_in_safe_mode: true 

development: 
    <<: *defaults 
    host: localhost 
    database: app_development 

test: 
    <<: *defaults 
    host: localhost 
    database: app_test 

production: 
    <<: *defaults 
    uri: <%= ENV['MONGOHQ_URL'] %> 

請注意,我用的Heroku,但我不使用MongoHQ添加上。我只是直接使用它,所以我手動設置我的MONGOHQ_URL。你的URI看起來是這樣的:

mongodb://<user>:<password>@<the.db.host.com>:<port>/<database_name> 

在我看來就像基於錯誤「localhost」的您無法連接(如也許你需要完整的主機名或IP還是什麼?)。你的應用程序日誌中有什麼?

只要確保不要在任何ENV上設置「host」和「uri」,因爲「host」會覆蓋從uri派生的設置。

+1

這樣做 - 非常感謝你!我正要把我的頭髮拉出來! :) – jschorr

相關問題