我正在尝试在 Node.js 中运行 MongoDB。我已经在 C:\mongodb 下安装了社区服务器版本的 MongoDB (v. 5.0.3)。
在子文件夹“data”中,我创建了另一个文件夹“db”,该文件夹目前是空的。
当我在 mongo.exe、Windows 终端甚至 Node.js 的终端中运行 mongoDB 时,一切似乎都正常。我可以查看我的数据库并创建新的数据库和集合。我还可以在 Compass 中访问我的数据库(连接 mongodb://localhost:27017)。
正如我所说,我的“data/db”文件夹是空的,即使我已经创建了一个新数据库,所以我的数据库似乎存储在其他地方。这是个问题吗?
在 Node.js 中,我尝试使用 Mongoose 连接到 MongoDB。我尝试了几种不同的方法并取得了不同程度的成功。此代码始终相同:
// Dependencies:
const express = require('express')
const app = express()
const mongoose = require('mongoose')
// Validate connection:
mongoose.connection.once('open', () => console.log('Connected do database'))
mongoose.connection.on('error', err => console.error('connection error:', err))
app.listen(5000, () => console.log('Server started'))
这个 URL 有效(貌似):
// Connecting to database:
mongoose.connect('mongodb://127.0.0.1:27017/testdb', { useNewUrlParser: true })
在控制台中,我看到“服务器已启动”,然后是“已连接到数据库”。
但是,YouTube 教程中建议的这个 URL 不起作用:
// Connecting to database:
mongoose.connect('mongodb://localhost/testdb', { useNewUrlParser: true })
在控制台中,我得到“服务器已启动”,但没有得到“已连接到数据库”。然后,半分钟后,应用程序崩溃,我收到以下错误消息(我已经审查了一些个人信息):
connection error: MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at NativeConnection.Connection.openUri (C:\Users\xxx\Documents\xxx\xxx\xxx\node_modules\mongoose\lib\connection.js:797:32)
at C:\Users\xxx\Documents\xxx\xxx\xxx\node_modules\mongoose\lib\index.js:330:10
at C:\Users\xxx\Documents\xxx\xxx\xxx\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (C:\Users\xxx\Documents\xxx\xxx\xxx\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (C:\Users\xxx\Documents\xxx\xxx\xxx\node_modules\mongoose\lib\index.js:1151:10)
at Mongoose.connect (C:\Users\xxx\Documents\xxx\xxx\xxx\node_modules\mongoose\lib\index.js:329:20)
at Object.<anonymous> (C:\Users\xxx\Documents\xxx\xxx\xxx\app.js:13:10)
at Module._compile (node:internal/modules/cjs/loader:1095:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
}
}
node:internal/process/promises:246
triggerUncaughtException(err, true /* fromPromise */);
^
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at NativeConnection.Connection.openUri (C:\Users\xxx\Documents\xxx\xxx\xxx\node_modules\mongoose\lib\connection.js:797:32)
at C:\Users\xxx\Documents\xxx\xxx\xxx\node_modules\mongoose\lib\index.js:330:10
at C:\Users\xxx\Documents\xxx\xxx\xxx\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (C:\Users\xxx\Documents\xxx\xxx\xxx\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (C:\Users\xxx\Documents\xxx\xxx\xxx\node_modules\mongoose\lib\index.js:1151:10)
at Mongoose.connect (C:\Users\xxx\Documents\xxx\xxx\xxx\node_modules\mongoose\lib\index.js:329:20)
at Object.<anonymous> (C:\Users\xxx\Documents\xxx\xxx\xxx\app.js:13:10)
at Module._compile (node:internal/modules/cjs/loader:1095:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'localhost:27017' => ServerDescription {
_hostAddress: HostAddress { isIPv6: false, host: 'localhost', port: 27017 },
address: 'localhost:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 4303888,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED ::1:27017
at connectionFailureError (C:\Users\xxx\Documents\xxx\xxx\xxx\node_modules\mongodb\lib\cmap\connect.js:293:20)
at Socket.<anonymous> (C:\Users\xxx\Documents\xxx\xxx\xxx\node_modules\mongodb\lib\cmap\connect.js:267:22)
at Object.onceWrapper (node:events:510:26)
at Socket.emit (node:events:390:28)
at emitErrorNT (node:internal/streams/destroy:164:8)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
}
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
}
}
Node.js v17.0.1
[nodemon] app crashed - waiting for file changes before starting...
是不是我的设置有误,因为我无法在本地主机上访问 MongoDB?
最佳答案
您的设置是正确的,但是 localhost
似乎解析为您的环回网络设备 ::1
的 IPv6 IP,而不是 IPv4 地址(127.0.0.1
)。我相信,默认情况下,MongoDB 绑定(bind)到 127.0.0.1 地址,这就是为什么您无法使用 ::1
地址访问它。
- 这里有几个选项:继续使用 127.0.0.1 IP -- 为什么不呢?
- 修改 mongodb 配置以绑定(bind)到
::1
。 - 更改
localhost
解析的内容。在 Linux 中,您为此编辑/etc/hosts
,Windows 不确定。
关于node.js - 无法在 Node.js 中访问本地主机上的 MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69921850/