当我尝试使用 Mongoose 将我的应用程序与数据库连接时遇到问题。已经尝试过以下我在谷歌上找到的解决方案:
但我无法解决它。谁能帮助我吗?
//my connection
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/notes-db-app',{
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(db => console.log('DB is connected'))
.catch(err => console.log(err));
然后抛出这个错误给我
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at NativeConnection.Connection.openUri (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\connection.js:797:32)
at C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:330:10 at C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:1151:10)
at Mongoose.connect (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:329:20)
at Object.<anonymous> (C:\Users\ivan\Desktop\NodeJS\notes-app\src\db.js:3: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
}
}
我尝试将端口放在我的连接代码上,如下所示
//my connection
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/notes-db-app',{
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(db => console.log('DB is connected'))
.catch(err => console.log(err));
它又给我带来了另一个错误
MongooseServerSelectionError: Invalid message size: 1347703880, max allowed: 67108864
at NativeConnection.Connection.openUri (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\connection.js:797:32)
at C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:330:10 at C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:1151:10)
at Mongoose.connect (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:329:20)
at Object.<anonymous> (C:\Users\ivan\Desktop\NodeJS\notes-app\src\db.js:3: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:3000' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
}
}
如果错误指出:
connect()错误:MongooseServerSelectionError:连接ECONNREFUSED ::1:27017
然后,在 IPv6 地址 ::1 上与 localhost 的连接被拒绝。 Mongoose 默认使用 IPv6 ..
为了快速检查,您可以显式设置 IPv4 地址:
mongoose.connect('mongodb://127.0.0.1/test')
只需传递第三个参数
family:4
即可。
mongoose.connect('mongodb://localhost/notes-db-app',{
useNewUrlParser: true,
useUnifiedTopology: true,
family: 4,
})
我终于解决了。
启用 MongoDB 默认禁用的 IPV6。在 CMD 上使用以下命令行:
mongod --ipv6
然后再次尝试连接,就可以了!
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/notes-db-app',{
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(db => console.log('DB is connected'))
.catch(err => console.log(err));
代表提问者发帖
const uri = 'mongodb://localhost:27017/test';
const options = {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 5000,
autoIndex: false, // Don't build indexes
maxPoolSize: 10, // Maintain up to 10 socket connections
serverSelectionTimeoutMS: 5000, // Keep trying to send operations for 5 seconds
socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
family: 4 // Use IPv4, skip trying IPv6
}
const connectWithDB = () => {
mongoose.connect(uri, options, (err, db) => {
if (err) console.error(err);
else console.log("database connection")
})
}
connectWithDB()
问题是,
localhost
别名解析为 IPv6 地址 ::1
而不是 127.0.0.1
net.ipv6
默认为 false
。
最好的选择是使用以下配置启动 MongoDB:
net:
ipv6: true
bindIpAll: true
或
net:
ipv6: true
bindIp: localhost
然后所有变体都将起作用:
mongoose.connect('mongodb://localhost:27017',{
useNewUrlParser: true,
useUnifiedTopology: true
})
mongoose.connect('mongodb://127.0.0.1:27017',{
useNewUrlParser: true,
useUnifiedTopology: true
})
mongoose.connect('mongodb://[::1]:27017',{
useNewUrlParser: true,
useUnifiedTopology: true
})
mongoose.connect(`mongodb://${os.hostname()}:27017`,{
useNewUrlParser: true,
useUnifiedTopology: true
})
如果您不将 MongoDB 作为服务运行,那么它会是
mongod --bind_ip_all --ipv6 <other options>
注意,我不喜欢配置
net:
bindIp: <ip_address>
在我看来,这仅在具有多个网络接口的计算机上才有意义,但是,MongoDB 不支持多个接口。如果您需要阻止来自远程计算机的任何连接(例如,在维护时或用作 Web 服务的后端数据库时),请使用
bindIp: localhost
,否则请使用 bindIpAll: true
可能您要连接的服务器的主机名/IP 设置不正确。
我习惯于将该错误视为:
MongooseServerSelectionError: connect ECONNREFUSED <hostname/hostIP>:<port>
并且在您发布的控制台日志中,
<hostname/hostIP>
部分格式错误/丢失。
示例 - 对于在端口27017上运行本地的 mongodb 服务器,这是服务器关闭时的错误:
MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
如果您使用 mongodb URI 连接到数据库,请确保它看起来像这样
"mongodb://<hostname/hostIP>:<port>"
在我的例子中,我刚刚更改了环境变量,其定义为:
DATABASE_LOCAL=mongodb://localhost:27017/db_name
前往线路:
DATABASE_LOCAL= mongodb://127.0.0.1:27017/db_name
mongod
const mongoose=require("mongoose");
mongoose.connect('mongodb://localhost/notes-db-app',{
useNewUrlParser: true,
useUnifiedTopology: true,
family: 4,
})