本地MongoDB服务器无法启动

时间:2018-02-16 11:50:51

标签: node.js mongodb express mongoose

我开始学习NodeJS和Express框架。我正在学习我有基本应用程序的教程

const express = require('express');
const exphbs = require('express-handlebars');
const mongoose = require('mongoose');

const app = express();

mongoose.Promise = global.Promise
// mongoose
mongoose.connect('mongodb://localhost/vidjot-dev')
.then(() => console.log("Mongo DB started"))
.catch(err => console.error(err));

// handlebars middleware
app.engine('handlebars', exphbs({
  defaultLayout: 'main'
}));
app.set('view engine','handlebars')


// index route
app.get('/', (req, res) =>{
  const title = "Welcome"
  res.render('index',{
    title : title
  });
});

app.get('/about', (req,res) =>{
  res.render('about');
});

const port = 5000;

app.listen(port, () => {
  console.log(`Server started on port ${port}`);
});

我尝试使用命令

从控制台启动节点js服务器
nodemon app

其中app是JS文件app.js的名称

当我尝试启动服务器时,出现此错误

{ MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
    at Pool.<anonymous> (/Users/sriramr/Desktop/node/vidjot/node_modules/mongodb-core/lib/topologies/server.js:503:11)
    at emitOne (events.js:116:13)
    at Pool.emit (events.js:211:7)
    at Connection.<anonymous> (/Users/sriramr/Desktop/node/vidjot/node_modules/mongodb-core/lib/connection/pool.js:326:12)
    at Object.onceWrapper (events.js:317:30)
    at emitTwo (events.js:126:13)
    at Connection.emit (events.js:214:7)
    at Socket.<anonymous> (/Users/sriramr/Desktop/node/vidjot/node_modules/mongodb-core/lib/connection/connection.js:245:50)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
  name: 'MongoNetworkError',
  message: 'failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]' }

我尝试再次设置数据库路径,但它没有任何用处。我试图从终端启动mongod,但又得到了同样的错误。我该如何解决这个问题?

编辑: 我创建了目录data / db,我刚刚运行

mongod --dbpath <DIRECTORY>

并收到此错误

2018-02-16T17:42:02.657+0530 I CONTROL  [initandlisten] MongoDB starting : pid=76227 port=27017 dbpath=/Users/sriramr/mongodb/data/db 64-bit host=MACBOOKs-MacBook-Air.local
2018-02-16T17:42:02.657+0530 I CONTROL  [initandlisten] db version v3.6.2
2018-02-16T17:42:02.657+0530 I CONTROL  [initandlisten] git version: 489d177dbd0f0420a8ca04d39fd78d0a2c539420
2018-02-16T17:42:02.657+0530 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 0.9.8zh 14 Jan 2016
2018-02-16T17:42:02.657+0530 I CONTROL  [initandlisten] allocator: system
2018-02-16T17:42:02.657+0530 I CONTROL  [initandlisten] modules: none
2018-02-16T17:42:02.657+0530 I CONTROL  [initandlisten] build environment:
2018-02-16T17:42:02.657+0530 I CONTROL  [initandlisten]     distarch: x86_64
2018-02-16T17:42:02.657+0530 I CONTROL  [initandlisten]     target_arch: x86_64
2018-02-16T17:42:02.657+0530 I CONTROL  [initandlisten] options: { storage: { dbPath: "/Users/sriramr/mongodb/data/db" } }
2018-02-16T17:42:02.659+0530 I -        [initandlisten] Detected data files in /Users/sriramr/mongodb/data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2018-02-16T17:42:02.660+0530 I STORAGE  [initandlisten] exception in initAndListen: InvalidOptions: Requested option conflicts with current storage engine option for directoryPerDB; you requested false but the current server storage is already set to true and cannot be changed, terminating
2018-02-16T17:42:02.661+0530 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
2018-02-16T17:42:02.661+0530 I NETWORK  [initandlisten] removing socket file: /tmp/mongodb-27017.sock
2018-02-16T17:42:02.661+0530 I CONTROL  [initandlisten] now exiting
2018-02-16T17:42:02.661+0530 I CONTROL  [initandlisten] shutting down with code:100

2 个答案:

答案 0 :(得分:1)

打开终端/ CMD并使用命令mongod

启动MongoDB服务器

此命令将为您启动MongoDB,因此您的应用可以通过localhost:27017

连接到它

答案 1 :(得分:0)

你确定你在运行mongo吗?您的URL指向localhost,这意味着您需要提供它(与使用nodemon提供的应用程序分开)。

此链接显示如何run mongo from shell