【问题标题】:Mongo::Error::NoServerAvailableMongo::Error::NoServerAvailable
【发布时间】:2018-07-24 20:07:16
【问题描述】:

我刚刚为 Rails 项目配置了 Mongoid,运行 rails g mongoid:config 将我的 mongoid.yml 编辑为如下所示。

development:
  clients:
    default:
      database: abacus_development
      # Provides the hosts the default client can connect to. Must be an array
      # of host:port pairs. (required)
      hosts: 
        - localhost
      options:

test:
  clients:
    default:
      database: abacus_test
      hosts:
        - localhost
      options:
        read:
          mode: primary
        max_pool_size: 1

然后继续认为一切都运行良好,搭建一个名为 Activity 的控制器和模型。于是我跑了rails s。该应用程序在localhost:3000 运行良好,但当我到达localhost:3000/activites 时,它会吐出这个。

ActionView::Template::Error (No server is available matching preference: #<Mongo::ServerSelector::Primary:0x007fbda47c4110 @tag_sets=[], @options={:database=>:abacus_development}, @server_selection_timeout=30>):
        15:   </thead>
        16:
        17:   <tbody>
        18:     <% @activities.each do |activity| %>
        19:       <tr>
        20:         <td><%= activity.amount %></td>
        21:         <td><%= activity.indicator %></td>   app/views/activities/index.html.erb:18:in `_app_views_activities_index_html_erb___73038992978597028_70226242222820'

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails mongodb


    【解决方案1】:

    您正在运行 mongo 对吗?如果您使用的是 Mac,可能使用“mongod”或“sudo service mongod start”。可能是一个愚蠢的答案,但你永远不知道

    【讨论】:

    • 是的,我是。但是sudo service mongod start 对我不起作用。我使用brew install mongodb 安装了 MongoDB,但似乎这可能存在问题,因为我什至无法执行 mongomongod 而不失败。
    • 我解决了。我没有在我的电脑上正确配置MongoDB,也没有设置启动代理,所以每次我需要使用它时,我需要同时做mongodmongo
    • sudo service mongodb start
    【解决方案2】:

    Mongo 默认在 27017 端口上运行,尝试在 mongoid.yml 中将主机配置更改为 localhost:27017。

    【讨论】:

      【解决方案3】:

      试试

      sudo mongod
      

      我觉得应该可以

      【讨论】:

        猜你喜欢
        • 2021-10-17
        • 1970-01-01
        • 2017-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-18
        • 2020-07-19
        • 2020-08-24
        相关资源
        最近更新 更多