Mongo::Error::NoServerAvailable when running Rails app
Mongo::Error::NoServerAvailable when running Rails app
当 运行 连接 rails s
并连接到 localhost:3000 时,我在尝试 @users.each
:
等简单查询时收到以下错误消息
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>
控制台显示
MONGODB | Adding localhost:3000 to the cluster
MONGODB | IOERROR
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:运行 mongo.exe
而在 C:\Program Files\MongoDB\Server.2\bin
中产生:
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
运行 mongod.exe
产量:
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
您正在尝试连接到 rails 端口上的 mongodb,正确的主机是 localhost:27017,对于本地服务器(如果数据库在本地运行则关闭)
我不得不手动
在 localhost:27017
将 config/mongoid.yml
更改为 运行
创建空目录C:data/db
然后 运行 mongod
(在我的具体情况下,导航到 C:\Program Files\MongoDB\Server.2\bin
和 运行 mongod.exe
)。
在那之后,启动 rails server
就像一个魅力。
我观察到类似的问题:
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!
我在互联网上搜索后发现,如果机器启用了 IPv6,则 Rails/mongoid 会尝试通过 IPv6 连接到 mongodb,但失败了。
我可以通过更改主机使其正常工作:mongoid.yml 中的条目来自
localhost:27017
到
(machine_ipv4_address):27017
当 运行 连接 rails s
并连接到 localhost:3000 时,我在尝试 @users.each
:
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>
控制台显示
MONGODB | Adding localhost:3000 to the cluster
MONGODB | IOERROR
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:运行 mongo.exe
而在 C:\Program Files\MongoDB\Server.2\bin
中产生:
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
运行 mongod.exe
产量:
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
您正在尝试连接到 rails 端口上的 mongodb,正确的主机是 localhost:27017,对于本地服务器(如果数据库在本地运行则关闭)
我不得不手动
在
localhost:27017
将 创建空目录
C:data/db
然后 运行
mongod
(在我的具体情况下,导航到C:\Program Files\MongoDB\Server.2\bin
和 运行mongod.exe
)。
config/mongoid.yml
更改为 运行
在那之后,启动 rails server
就像一个魅力。
我观察到类似的问题:
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!
我在互联网上搜索后发现,如果机器启用了 IPv6,则 Rails/mongoid 会尝试通过 IPv6 连接到 mongodb,但失败了。
我可以通过更改主机使其正常工作:mongoid.yml 中的条目来自
localhost:27017
到
(machine_ipv4_address):27017