简体   繁体   English

无法使用 Node.js 创建 MongoDB 数据库

[英]Unable to create MongoDB database using Node.js

I am trying to run the below program to create a mongo database using Node.js by running node app.js .我正在尝试运行以下程序,通过运行node app.js使用 Node.js 创建一个 mongo 数据库。

app.js应用程序.js

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

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

Below is the error I'm getting:-以下是我收到的错误:-

(node:20815) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.

MongoNetworkError: failed to connect to server [hostname_fqdn:27017] on first connect [Error: connect ECONNREFUSED 10.127.45.59:27017
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1134:16) {
  name: 'MongoNetworkError'
}]
    at Pool.<anonymous> (/root/myfolder/node_modules/mongodb/lib/core/topologies/server.js:438:11)
    at Pool.emit (events.js:223:5)
    at /root/myfolder/node_modules/mongodb/lib/core/connection/pool.js:562:14
    at /root/myfolder/node_modules/mongodb/lib/core/connection/pool.js:995:11
    at /root/myfolder/node_modules/mongodb/lib/core/connection/connect.js:32:7
    at callback (/root/myfolder/node_modules/mongodb/lib/core/connection/connect.js:280:5)
    at Socket.<anonymous> (/root/myfolder/node_modules/mongodb/lib/core/connection/connect.js:310:7)
    at Object.onceWrapper (events.js:313:26)
    at Socket.emit (events.js:223:5)
    at emitErrorNT (internal/streams/destroy.js:92:8) {
  name: 'MongoNetworkError'
}

The file/node_modules/package.json all are located in a CentOS Virtual Machine.文件/node_modules/package.json 都位于 CentOS 虚拟机中。

You need to start the MongoDB Service after installing.安装后需要启动MongoDB服务。

Edited:编辑:

If you have already started service and connecting mongo via terminal, then try to remove mongo lock file (rm /var/lib/mongodb/mongod.lock) and repair mongod (mongod -–repair).如果您已经启动服务并通过终端连接 mongo,则尝试删除 mongo 锁定文件 (rm /var/lib/mongodb/mongod.lock) 并修复 mongod (mongod --repair)。 Now start mongo service and see if you can connect.现在启动mongo服务,看看能不能连上。 I had similar issue with EC2 and Compass and tried above to resolve.我在 EC2 和 Compass 上遇到了类似的问题,并尝试在上面解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM