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

时间:2013-06-28 09:36:27

标签: ruby-on-rails mongodb mongoid

我已经在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

默认值:&默认值               主持人:localhost

        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:

1 个答案:

答案 0 :(得分:1)

尝试

RAILS_ENV=production rails c

或@Rubyman建议

rails c production

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

的环境