Ruby on rails 运行Rails应用程序时,Mongo::Error::NoServerAvailable
运行Ruby on rails 运行Rails应用程序时,Mongo::Error::NoServerAvailable,ruby-on-rails,mongodb,mongoid,Ruby On Rails,Mongodb,Mongoid,运行rails s并连接到localhost:3000时,我在尝试诸如@users之类的简单查询时收到以下错误消息。每个: Mongo::Error::NoServerAvailable in Users#index No server is available matching preference: #<Mongo::ServerSelector::Primary:0x62403672 tag_sets=[] server_selection_timeout=30 local_thr
rails s
并连接到localhost:3000时,我在尝试诸如@users之类的简单查询时收到以下错误消息。每个:
Mongo::Error::NoServerAvailable in Users#index
No server is available matching preference: #<Mongo::ServerSelector::Primary:0x62403672 tag_sets=[] server_selection_timeout=30 local_threshold=0.015>
config/mongoid.yml
development:
clients:
default:
database: virtu_development
hosts:
- localhost:3000
options:
options:
test:
clients:
default:
database: virtu_test
hosts:
- localhost:3000
options:
read:
mode: :primary
max_pool_size: 1
编辑:当运行时将mongoid.yml设置为在localhost:27017上运行时,控制台会重复读取:
No connection could be made because the target machine actively refused it. - connect(2) for 127.0.0.1:27017
编辑2:在C:\Program Files\MongoDB\Server\3.2\bin中运行mongo.exe
会产生:
MongoDB shell version: 3.2.4
connecting to: test
2016-03-31T19:14:03.597-0400 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:10061 No connection could be made because the target machine actively refused it.
2016-03-31T19:14:03.597-0400 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:224:14
@(connect):1:6
2016-03-31T19:13:00.636-0400 I CONTROL [initandlisten] MongoDB starting : pid=5564 port=27017 dbpath=C:\data\db\ 64-bit host=Kalyn-PC
2016-03-31T19:13:00.640-0400 I CONTROL [initandlisten] targetMinOS: Windows Vista/Windows Server 2008
2016-03-31T19:13:00.640-0400 I CONTROL [initandlisten] db version v3.2.4
2016-03-31T19:13:00.640-0400 I CONTROL [initandlisten] git version: e2ee9ffcf9f5a94fad76802e28cc978718bb7a30
2016-03-31T19:13:00.640-0400 I CONTROL [initandlisten] allocator: tcmalloc
2016-03-31T19:13:00.640-0400 I CONTROL [initandlisten] modules: none
2016-03-31T19:13:00.640-0400 I CONTROL [initandlisten] build environment:
2016-03-31T19:13:00.641-0400 I CONTROL [initandlisten] distarch: x86_64
2016-03-31T19:13:00.641-0400 I CONTROL [initandlisten] target_arch: x86_64
2016-03-31T19:13:00.641-0400 I CONTROL [initandlisten] options: {}
2016-03-31T19:13:00.642-0400 I STORAGE [initandlisten] exception in initAndListen: 29 Data directory C:\data\db\ not found., terminating
2016-03-31T19:13:00.642-0400 I CONTROL [initandlisten] dbexit: rc: 100
运行mongod.exe
会产生:
MongoDB shell version: 3.2.4
connecting to: test
2016-03-31T19:14:03.597-0400 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:10061 No connection could be made because the target machine actively refused it.
2016-03-31T19:14:03.597-0400 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:224:14
@(connect):1:6
2016-03-31T19:13:00.636-0400 I CONTROL [initandlisten] MongoDB starting : pid=5564 port=27017 dbpath=C:\data\db\ 64-bit host=Kalyn-PC
2016-03-31T19:13:00.640-0400 I CONTROL [initandlisten] targetMinOS: Windows Vista/Windows Server 2008
2016-03-31T19:13:00.640-0400 I CONTROL [initandlisten] db version v3.2.4
2016-03-31T19:13:00.640-0400 I CONTROL [initandlisten] git version: e2ee9ffcf9f5a94fad76802e28cc978718bb7a30
2016-03-31T19:13:00.640-0400 I CONTROL [initandlisten] allocator: tcmalloc
2016-03-31T19:13:00.640-0400 I CONTROL [initandlisten] modules: none
2016-03-31T19:13:00.640-0400 I CONTROL [initandlisten] build environment:
2016-03-31T19:13:00.641-0400 I CONTROL [initandlisten] distarch: x86_64
2016-03-31T19:13:00.641-0400 I CONTROL [initandlisten] target_arch: x86_64
2016-03-31T19:13:00.641-0400 I CONTROL [initandlisten] options: {}
2016-03-31T19:13:00.642-0400 I STORAGE [initandlisten] exception in initAndListen: 29 Data directory C:\data\db\ not found., terminating
2016-03-31T19:13:00.642-0400 I CONTROL [initandlisten] dbexit: rc: 100
您正在尝试连接到mongodb on rails端口,正确的主机是localhost:27017,对于本地服务器(如果db在本地运行,则不正常)我必须手动
更改config/mongoid.yml
以在localhost:27017运行
创建空目录C:data/db
然后运行mongod
(在我的特定情况下,导航到C:\Program Files\MongoDB\Server\3.2\bin
并运行mongod.exe
)
在那之后,启动rails服务器
工作得非常顺利。我发现了一个类似的问题:
MONGODB | Errno::ECONNREFUSED: Connection refused - connect(2) for 127.0.0.1:27017
Rendered gdd_apis/index.html.erb within layouts/application (60062.0ms)
Completed 500 Internal Server Error in 60075ms
ActionView::Template::Error (No server is available matching preference: #<Mongo::ServerSelector::Primary:0x27416600 tag_sets=[] max_staleness=nil> using server_selection_timeout=30 and local_threshold=0.015):
19: </tr>
20: </thead>
21: <tbody>
22: <% if @gdd_apis.size == 0 %>
23: <tr>
24: <pis/tr>
25: <% else %>
app/views/gdd_apis/index.html.erb:22:in `_app_views_gdd_apis_index_html_erb___3636122846566909968_28792560'
- Gracefully stopping, waiting for requests to finish
=== puma shutdown: 2018-08-31 14:04:39 -0500 ===
- Goodbye!
MONGODB | Errno::EconRefused:连接被拒绝-127.0.0.1:27017的连接(2)
布局/应用程序中呈现的gdd_API/index.html.erb(600062.0ms)
在60075ms内完成500个内部服务器错误
ActionView::Template::Error(没有可用的服务器匹配首选项:#使用服务器_选择_超时=30和本地_阈值=0.015):
19:
20:
21:
22:
23:
24:
25:
app/views/gdd_-api/index.html.erb:22:in``应用程序视图\ gdd_-api \索引\ html \ erb \ 3636122846566909968 \ 28792560'
-优雅地停止,等待请求完成
puma关闭时间:2018-08-3114:04:39-0500===
-再见!
我在互联网上搜索后发现,如果机器启用了IPv6,那么Rails/mongoid会尝试通过IPv6连接到mongodb,但失败了
我可以通过更改hosts:mongoid.yml中的条目使其工作
本地主机:27017
到
(machine_ipv4_address):27017已尝试此操作--我在控制台中收到错误消息无法建立连接,因为目标计算机主动拒绝了它。-为127.0.0.1:27017连接(2)
您确定您的计算机上正在运行mongodb服务器吗?看起来你不知道。。。我安装了它。我的应用程序上安装了mongoid
gem。不过,在命令行中运行mongo
并没有任何作用。我将尝试修改我的系统路径。使用运行mongo.exe
和mongod.exe
的错误输出更新了我的原始帖子。这似乎是一个更容易被谷歌发现的问题,所以我将独自开始这一行的研究。另外:检查您已将mongo设置为使用哪个文件夹,并确保权限正常。我在/data/db中安装了mongo,操作系统更新删除了文件夹上所需的权限关于mac?您不必导航到将存储数据的文件夹,只需将路径作为参数传递,如:--dbpath“path_TO_your_folder”