node.js Mongodb:第一次连接时无法连接到服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41318354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mongodb: failed to connect to server on first connect
提问by Michael Kr?yserth-Sims?
I get the following error:
我收到以下错误:
Warning { MongoError: failed to connect to server [mongodb:27017] on first connect
at Pool.<anonymous> (/Users/michaelks/Desktop/users/node_modules/mongodb-core/lib/topologies/server.js:325:35)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection.<anonymous> (/Users/michaelks/Desktop/users/node_modules/mongodb-core/lib/connection/pool.js:270:12)
at Connection.g (events.js:292:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
at Socket.<anonymous> (/Users/michaelks/Desktop/users/node_modules/mongodb-core/lib/connection/connection.js:173:49)
at Socket.g (events.js:292:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at connectErrorNT (net.js:1025:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
name: 'MongoError',
message: 'failed to connect to server [mongodb:27017] on first connect' }
Even though I get this in the terminal window where I run Mongo:
即使我在运行 Mongo 的终端窗口中得到了这个:
2016-12-25T03:45:23.715+0100 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58868 #8 (8 connections now open)
It kinda looks like there is a connection..
好像有联系。。
I've tried both
我都试过了
$ mongod
and
和
$ brew services start mongo
This is my test_helper.js
这是我的 test_helper.js
const mongoose = require('mongoose');
mongoose.connect('mongodb:localhost/users_test');
mongoose.connection
.once('open', () => console.log('Good to go!'))
.on('error', (error) => {
console.warn('Warning', error);
});
I have not specifically made the db "users_test" as I am under the impression that mongoose or mongo or both will do this on the fly.
我没有专门制作数据库“users_test”,因为我的印象是猫鼬或 mongo 或两者都会即时执行此操作。
I've tried both "localhost" and "127.0.0.1?". I'm on OSX 10.11.5 I'm running Node 7.3.0 and Mongo 3.2.9
我试过“本地主机”和“127.0.0.1?”。我在 OSX 10.11.5 上运行 Node 7.3.0 和 Mongo 3.2.9
What am I doing wrong? How do I figure out what's wrong?
我究竟做错了什么?我怎么知道出了什么问题?
采纳答案by Bertrand Martel
To connectto mongodbwith mongoose, you can use :
要连接到mongodb使用mongoose,你可以使用:
mongoose.connect('mongodb://localhost/users_test');
or
或者
mongoose.connect('localhost/users_test');
or
或者
mongoose.connect('localhost','users_test');
But not mongoose.connect('mongodb:localhost/users_test');, it doesnt match the right hostname (mongodbinstead of localhost)
但不是mongoose.connect('mongodb:localhost/users_test');,它不匹配正确的主机名(mongodb而不是localhost)
回答by xameeramir
Adding one more answer as the solution is from different contexts:
添加另一个答案,因为解决方案来自不同的上下文:
MongoDb Atlas problem solution
MongoDb Atlas 问题解决方案
I connected to MongoDB Atlas and whitelisted my machine's IP by following the below steps:
我连接到 MongoDB Atlas 并按照以下步骤将我的机器的 IP 列入白名单:
- Go to the security tab
- 转到安全选项卡
- Go to the network access tab under the security tab
- 转到安全选项卡下的网络访问选项卡
- Go to the IP whitelist tab
- 转到 IP 白名单选项卡
- Enter the IP address of your machine
回答by Mohit Turkar
I was getting same error while I tried to connect mlab db that is because my organization network has some restriction, I switched to mobile hotspot and it worked.
我在尝试连接 mlab db 时遇到同样的错误,这是因为我的组织网络有一些限制,我切换到移动热点并且它工作正常。
回答by Basheer AL-MOMANI
I ran into this issue serval times, and here is some troubleshooting list
我多次遇到这个问题,这里有一些 troubleshooting list
- make sure
database pathisexist, the default path in windows inC:\data\db - make sure
mongo is running, to run it go toC:\Program Files\MongoDB\Server\3.4\binand run the following:mongod.exemongo.exe
- make sure that your
connection string is correctlikemongodb://localhost/database-name
- 确保
database path是exist,windows 中的默认路径C:\data\db - 确保
mongo is running,要运行它,请转到C:\Program Files\MongoDB\Server\3.4\bin并运行以下命令:mongod.exemongo.exe
- 确保你
connection string is correct喜欢mongodb://localhost/database-name
回答by shaheb
if it is cluster MongoDB then you need to add your current IP to the cluster, to add your current IP address you need to complete few steps below-
如果是集群 MongoDB 那么您需要将您当前的 IP 添加到集群中,要添加您当前的 IP 地址,您需要完成以下几个步骤-
step 1- go to login page of Mongodb and login with valid credential - https://cloud.mongodb.com/user#/atlas/login
step 2- CLick Network Access from left sidebar under Security Section
Step 3 - Click Add IP Address
Step 4 - Click Add Current IP Address or Allow Connection From Any Where
now try to connect - npm start
and for Local MongoDB use mongo String like "mongodb+srv://username:pass%[email protected]/test?retryWrites=true&w=majority"
对于本地 MongoDB 使用 mongo 字符串,如“mongodb+srv://username:pass%[email protected]/test?retryWrites=true&w=majority”
password must be encoded like example string
密码必须像示例字符串一样编码
回答by Isaac Sekamatte
While connected to a wifi network, mongodb://localhost/db_nameworked as expected.
连接到 wifi 网络时,mongodb://localhost/db_name按预期工作。
When I wasn't connected to any wifi network, this couldn't work. Instead I used,
mongodb://127.0.0.1/db_nameand it worked.
当我没有连接到任何 wifi 网络时,这不起作用。相反,我使用了,
mongodb://127.0.0.1/db_name并且奏效了。
Probably a problem to do with ip configurations.
可能是 ip 配置的问题。
回答by Pooja S Arkasali
I was trying to connect mlab with my company wifi connection and it was giving this error. But when I switched to personal network it worked and got connected.
我试图将 mlab 与我公司的 wifi 连接连接,但出现此错误。但是当我切换到个人网络时,它工作并连接上了。
回答by Sai Prasad
For me,issue was mongo was not running.So first i started "mongod" command in the console.And later in another console tab I have run "mongo". Now the connection is successful. Now run your app your problem should be solved.
对我来说,问题是 mongo 没有运行。所以首先我在控制台中启动了“mongod”命令。后来在另一个控制台选项卡中我运行了“mongo”。现在连接成功。现在运行您的应用程序,您的问题应该得到解决。
回答by Mike Veazie - MSFT
I was running mongod in a PowerShell instance. I was not getting any output in the powershell console from mongod. I clicked on the PowerShell instance where mongod was running, hit enter and and execution resumed. I am not sure what caused the execution to halt, but I can connect to this instance of mongo immediately now.
我在 PowerShell 实例中运行 mongod。我在 mongod 的 powershell 控制台中没有得到任何输出。我单击了 mongod 正在运行的 PowerShell 实例,按 Enter 并恢复执行。我不确定是什么导致执行停止,但我现在可以立即连接到这个 mongo 实例。
回答by xameeramir
Local MongoDb problem solution
本地MongoDb问题解决方案
solve it by following the below steps:
按照以下步骤解决:
Changed
改变了
mongodb://localhost:27017/local
to
到
localhost/local
And the error gone
错误消失了


