2

我已经在 ec2 实例上部署了我的 rails 应用程序,使用的数据库是 Mongo 并托管在一个单独的实例上。mongoid.yml 中的数据库和主机已相应更改,指向 db 服务器。该应用程序正在生产模式下运行并且工作正常。当我在生产模式下访问 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:
4

1 回答 1

1

尝试

RAILS_ENV=production rails c

或如@Rubyman 建议的那样

rails c production

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

于 2013-06-28T10:14:58.963 回答