Node.js MongoDB创建数据库给出错误

时间:2017-06-12 14:00:05

标签: javascript node.js mongodb

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";

MongoClient.connect(url, function(err, db) {
  if (err) 
  {
  throw err;
  }
  console.log("Database created!");
  db.close();
});

这是我在nodejs中执行此代码并尝试创建数据库时遇到的错误,我正在浏览w3school中的教程,请帮助解决此错误 -

    C:\Users\n.patel\Desktop\Promotion\nodejs\application4success>node mymongodb.js

C:\Users\n.patel\Desktop\Promotion\nodejs\application4success\node_modules\mongodb\lib\mongo_client.js:415
          throw err
          ^
 MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
    at null.<anonymous> (C:\Users\n.patel\Desktop\Promotion\nodejs\application4success\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:3
29:35)
    at emitOne (events.js:77:13)
    at emit (events.js:169:7)
    at null.<anonymous> (C:\Users\n.patel\Desktop\Promotion\nodejs\application4success\node_modules\mongodb\node_modules\mongodb-core\lib\connection\pool.js:280
:12)
    at g (events.js:260:16)
    at emitTwo (events.js:87:13)
    at emit (events.js:172:7)
    at Socket.<anonymous> (C:\Users\n.patel\Desktop\Promotion\nodejs\application4success\node_modules\mongodb\node_modules\mongodb-core\lib\connection\connection.js:187:49)
    at Socket.g (events.js:260:16)
    at emitOne (events.js:77:13)

1 个答案:

答案 0 :(得分:0)

确保您在端口27017上运行Mongo 用这个来验证

mongo localhost:27017