mongodb MongoError:第一次连接时无法连接到服务器 [localhost:27017]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41611146/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:52:45  来源:igfitidea点击:

MongoError: failed to connect to server [localhost:27017] on first connect

mongodblocalhostmean-stack

提问by Sachin

I have paid a membership and downloaded this project. After npm installand node app.js, it returns the following error:

我已经支付了会员资格并下载了这个项目。在npm installand之后node app.js,它返回以下错误:

Does anyone know what's wrong?

有谁知道出了什么问题?

/Users/Thomas/Desktop/mean-stack-tutorial-01$ node app.js 

/Users/Thomas/Desktop/mean-stack-tutorial-01/node_modules/mongodb/lib/server.js:265
        process.nextTick(function() { throw err; })
                                      ^
MongoError: failed to connect to server [localhost:27017] on first connect
    at Pool.<anonymous> (/Users/Thomas/Desktop/mean-stack-tutorial-01/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/Thomas/Desktop/mean-stack-tutorial-01/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/Thomas/Desktop/mean-stack-tutorial-01/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 emitErrorNT (net.js:1281:8)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

Edit 1:Following the comments, I just installed mongo via brew, and did brew services start mongodband then mongo:

编辑1:下面的评论,我只是通过蒙哥安装brew,并没有brew services start mongodbmongo

/Users/Thomas$ mongo
MongoDB shell version v3.4.1
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.1
Server has startup warnings: 
2017-01-12T19:53:01.868+0800 I CONTROL  [initandlisten] 
2017-01-12T19:53:01.869+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-01-12T19:53:01.869+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-01-12T19:53:01.869+0800 I CONTROL  [initandlisten] 

Then I did

然后我做了

/Users/Thomas$ mongod -httpinterface --dbpath /Users/Thomas/Desktop/MongoDB/db
2017-01-12T20:14:05.620+0800 I CONTROL  [initandlisten] MongoDB starting : pid=47356 port=27017 dbpath=/Users/Thomas/Desktop/MongoDB/db 64-bit host=Pro.local
2017-01-12T20:14:05.621+0800 I CONTROL  [initandlisten] db version v3.4.1
2017-01-12T20:14:05.621+0800 I CONTROL  [initandlisten] git version: 5e103c4f5583e2566a45d740225dc250baacfbd7
2017-01-12T20:14:05.621+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2j  26 Sep 2016
2017-01-12T20:14:05.621+0800 I CONTROL  [initandlisten] allocator: system
2017-01-12T20:14:05.621+0800 I CONTROL  [initandlisten] modules: none
2017-01-12T20:14:05.621+0800 I CONTROL  [initandlisten] build environment:
2017-01-12T20:14:05.621+0800 I CONTROL  [initandlisten]     distarch: x86_64
2017-01-12T20:14:05.621+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2017-01-12T20:14:05.621+0800 I CONTROL  [initandlisten] options: { storage: { dbPath: "/Users/Thomas/Desktop/MongoDB/db" } }
2017-01-12T20:14:05.657+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=3584M,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2017-01-12T20:14:14.108+0800 I CONTROL  [initandlisten] 
2017-01-12T20:14:14.108+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-01-12T20:14:14.108+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-01-12T20:14:14.108+0800 I CONTROL  [initandlisten] 
2017-01-12T20:14:14.108+0800 I CONTROL  [initandlisten] 
2017-01-12T20:14:14.109+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2017-01-12T20:14:15.390+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/Users/Thomas/Desktop/MongoDB/db/diagnostic.data'
2017-01-12T20:14:16.303+0800 I INDEX    [initandlisten] build index on: admin.system.version properties: { v: 2, key: { version: 1 }, name: "incompatible_with_version_32", ns: "admin.system.version" }
2017-01-12T20:14:16.304+0800 I INDEX    [initandlisten]      building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2017-01-12T20:14:16.406+0800 I INDEX    [initandlisten] build index done.  scanned 0 total records. 0 secs
2017-01-12T20:14:16.407+0800 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.4
2017-01-12T20:14:16.407+0800 I NETWORK  [thread1] waiting for connections on port 27017

Then in another terminal, I did node app.js, which hangs (and does not print anything). Then I does http://localhost:28017in a browser, it seems to work:

然后在另一个终端中,我做了node app.js,它挂起(并且不打印任何东西)。然后我http://localhost:28017在浏览器中做,它似乎工作:

enter image description here

在此处输入图片说明

However, how could I see the website?

但是,我怎么能看到网站呢?

Edit 2:

编辑2:

The normal process is

正常流程是

  1. install mongo

  2. brew services start mongodbin a terminal

  3. mongo &(assume we use the default folder /data/db/)

  4. npm start

  5. see e.g., bin/wwwfor the port number, then use localhost:the_port_numberin a browser

  1. 安装 mongo

  2. brew services start mongodb在一个终端

  3. mongo &(假设我们使用默认文件夹/data/db/

  4. npm start

  5. 参见例如bin/www端口号,然后localhost:the_port_number在浏览器中使用

回答by Sachin

Update 2020 Feb 24:

2020 年 2 月 24 日更新:

It would be wise to stay tuned with it. Here is the link for latest library instruction:

继续关注它是明智的。这是最新库说明的链接:

https://docs.mongodb.com/manual/administration/install-community/

https://docs.mongodb.com/manual/administration/install-community/



You haven't started your monogo database. first install mongo install mongo as per your OS https://docs.mongodb.com/v3.0/tutorial/

您还没有启动您的 monogo 数据库。首先根据您的操作系统安装 mongo安装 mongo https://docs.mongodb.com/v3.0/tutorial/

then follow instructions mentioned on this site: https://docs.mongodb.com/v3.0/tutorial/getting-started-with-the-mongo-shell/

然后按照本网站上提到的说明进行操作:https: //docs.mongodb.com/v3.0/tutorial/getting-started-with-the-mongo-shell/

  1. cd .
  2. mongod --dbpath "any directory path to start your database "
  1. 光盘。
  2. mongod --dbpath "启动数据库的任何目录路径"

and then start your node server. It will work fine.

然后启动您的节点服务器。它会正常工作。

Hope this helps!!

希望这可以帮助!!

回答by Shiv Prakash

First open another terminal an run command mongodto start your mongoDB

首先打开另一个终端运行命令mongod来启动你的mongoDB

Then again start you node server npm startor if you have "nodemon" then using nodemon

然后再次启动您的节点服务器,npm start或者如果您有“nodemon”,则使用nodemon

I think now everything will be fine it will show

我想现在一切都会好起来的

Mongoose connected to mongodb://localhost/your_database

回答by user7982813

I had the same issue but it worked fine after restarting the service

我有同样的问题,但重新启动服务后它工作正常

service mongod start

service mongod start