【发布时间】:2021-01-07 08:07:24
【问题描述】:
我正在尝试通过运行node app.js 来运行以下程序以使用 Node.js 创建一个 mongo 数据库。
app.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();
});
以下是我遇到的错误:-
(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'
}
文件/node_modules/package.json 都位于 CentOS 虚拟机中。
【问题讨论】:
-
您的网址似乎不正确或数据库未运行。
-
您是否在主机名:27017 上安装并运行了 mongodb?
-
Mongo 正在运行,因为我能够手动创建数据库/集合。我已经在 CentOS 虚拟机上安装了 mongodb。
-
hostname只是我在 SO 中提供的一个虚拟主机。我已输入 FQDN 作为主机名。并且 MongoDB 正在运行 hostname:27017 -
我已经在 admin db 中创建了用户,但是我无法通过使用 MongoDB Compass 提供凭据来连接到 MongoDB
标签: javascript node.js mongodb