gpt4 book ai didi

ruby-on-rails - 从rails控制台连接到不同服务器上的mongodb

转载 作者:可可西里 更新时间:2023-11-01 10:44:18 25 4
gpt4 key购买 nike

我已经在 ec2 实例上部署了我的 Rails 应用程序,使用的数据库是 Mongo 并托管在一个单独的实例上。mongoid.yml 中的数据库和主机已相应更改,指向数据库服务器。该应用程序正在生产模式下运行并且运行良好。当我在生产模式下访问 Rails 控制台时,

           $rails c RAILS_ENV=production

并尝试访问模型 User.last。它给出了错误

    Loading RAILS_ENV=production environment (Rails 3.2.11)
1.9.3p327 :001 > User.last
Mongoid::Errors::NoSessionsConfig:
Problem:
No sessions configuration provided.
Summary:
Mongoid's configuration requires that you provide details about each session that can be connected to, and requires in the sessions config at least 1 default session to exist.
Resolution:
Double check your mongoid.yml to make sure that you have a top-level sessions key with at least 1 default session configuration for it. You can regenerate a new mongoid.yml for assistance via `rails g mongoid:config`.

Example:
  development:
    sessions:
      default:
        database: mongoid_dev
        hosts:
          - localhost:27017


from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/sessions/factory.rb:61:in `create_session'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/sessions/factory.rb:43:in `default'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/sessions.rb:109:in `default'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/sessions.rb:378:in `__session__'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/sessions.rb:213:in `mongo_session'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/sessions.rb:157:in `collection'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/contextual/mongo.rb:216:in `initialize'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/contextual.rb:47:in `new'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/contextual.rb:47:in `create_context'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/contextual.rb:30:in `context'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/contextual.rb:18:in `last'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.14/lib/mongoid/finders.rb:129:in `last'
from (irb):1
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'

Mongoid.yml

默认值:&defaults 主机:本地主机

        development:
# Configure available database sessions. (required)
sessions:
# Defines the default session. (required)
default:
# Defines the name of the default database that Mongoid can connect to.
# (required).
database: app_db
# Provides the hosts the default session can connect to. Must be an array
# of host:port pairs. (required)
hosts:
- localhost:27017
options:

options:

test:
sessions:
default:
database: app_db
hosts:
- localhost:27017
options:
consistency: :strong
# In the test environment we lower the retries and retry interval to
# low amounts for fast failures.
max_retries: 1
retry_interval: 0


production:
# Configure available database sessions. (required)
sessions:
# Defines the default session. (required)
default:
# Defines the name of the default database that Mongoid can connect to.
# (required).
database: app_db
# Provides the hosts the default session can connect to. Must be an array
# of host:port pairs. (required)
hosts:
- ec2-**-***-***-*.ap-southeast-1.compute.amazonaws.com:27017
options:

options:

最佳答案

尝试

RAILS_ENV=production rails c

或者按照@Rubyman的建议

rails c production

您的命令的问题是 mongoid 正在搜索名为 RAILS_ENV=production 的环境

关于ruby-on-rails - 从rails控制台连接到不同服务器上的mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17361637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com