我当前的Mongoose连接方法无法连接。我可以毫无问题地连接到外壳中的数据库:
mongo --username 'admin' --password 'mypassword' --authenticationDatabase 'admin'
这是它提供的输出:
MongoDB shell version v4.2.6
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("d5275c30-cbc6-413f-8dc3-31851219f64b") }
MongoDB server version: 4.2.6
但是,当我尝试使用以前可用的Mongoose连接方法时,它停滞了并且没有真正连接。这是我在猫鼬中使用的连接方法:
async () => {
let options = {
useNewUrlParser: true,
useUnifiedTopology: true,
keepAlive: true,
user: process.env.MONGODB_USER,
pass: process.env.MONGO_PASS,
};
try {
await mongoose.connect(process.env.MONGODB_URI, options);
} catch (err) {
logger.error("Could not connect to DB.");
logger.error(err);
process.exit(1);
}
const db = mongoose.connection;
db.on("error", (err) => {
logger.error("Error occured in MongoDB.", err);
});
db.on("disconnected", () => {
logger.error("Connection to MongoDB closed.");
});
db.once("open", () => {
logger.info("Connection to MongoDB opened.");
});
return db;
}
最终,它给了我这个错误(即使我使用的是与从命令行使用的相同的凭据):
Error occured in MongoDB.Authentication failed.
(node:29648) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: Authentication failed.
at new MongooseServerSelectionError (/home/harrison/graphQlServer/source/node_modules/mongoose/lib/error/serverSelection.js:24:11)
at NativeConnection.Connection.openUri (/home/harrison/graphQlServer/source/node_modules/mongoose/lib/connection.js:823:32)
at Mongoose.connect (/home/harrison/graphQlServer/source/node_modules/mongoose/lib/index.js:333:15)
at _callee$ (/home/harrison/graphQlServer/source/server/mongodb/connect/index.js:14:14)
at tryCatch (/home/harrison/graphQlServer/source/node_modules/regenerator-runtime/runtime.js:45:40)
at Generator.invoke [as _invoke] (/home/harrison/graphQlServer/source/node_modules/regenerator-runtime/runtime.js:274:22)
at Generator.prototype.<computed> [as next] (/home/harrison/graphQlServer/source/node_modules/regenerator-runtime/runtime.js:97:21)
at asyncGeneratorStep (/home/harrison/graphQlServer/source/server/mongodb/connect/index.js:14:103)
at _next (/home/harrison/graphQlServer/source/server/mongodb/connect/index.js:16:194)
at /home/harrison/graphQlServer/source/server/mongodb/connect/index.js:16:364
at new Promise (<anonymous>)
at /home/harrison/graphQlServer/source/server/mongodb/connect/index.js:16:97
at connect (/home/harrison/graphQlServer/source/server/mongodb/connect/index.js:4:21)
at _callee$ (/home/harrison/graphQlServer/source/server/index.js:9:9)
at tryCatch (/home/harrison/graphQlServer/source/node_modules/regenerator-runtime/runtime.js:45:40)
at Generator.invoke [as _invoke] (/home/harrison/graphQlServer/source/node_modules/regenerator-runtime/runtime.js:274:22)
(node:29648) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:29648) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:29648) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: Authentication failed.
at new MongooseServerSelectionError (/home/harrison/graphQlServer/source/node_modules/mongoose/lib/error/serverSelection.js:24:11)
at NativeConnection.Connection.openUri (/home/harrison/graphQlServer/source/node_modules/mongoose/lib/connection.js:823:32)
at Mongoose.connect (/home/harrison/graphQlServer/source/node_modules/mongoose/lib/index.js:333:15)
at _callee$ (/home/harrison/graphQlServer/source/server/mongodb/connect/index.js:14:14)
at tryCatch (/home/harrison/graphQlServer/source/node_modules/regenerator-runtime/runtime.js:45:40)
at Generator.invoke [as _invoke] (/home/harrison/graphQlServer/source/node_modules/regenerator-runtime/runtime.js:274:22)
at Generator.prototype.<computed> [as next] (/home/harrison/graphQlServer/source/node_modules/regenerator-runtime/runtime.js:97:21)
at asyncGeneratorStep (/home/harrison/graphQlServer/source/server/mongodb/connect/index.js:14:103)
at _next (/home/harrison/graphQlServer/source/server/mongodb/connect/index.js:16:194)
at /home/harrison/graphQlServer/source/server/mongodb/connect/index.js:16:364
at new Promise (<anonymous>)
at /home/harrison/graphQlServer/source/server/mongodb/connect/index.js:16:97
at connect (/home/harrison/graphQlServer/source/server/mongodb/connect/index.js:4:21)
at _callee$ (/home/harrison/graphQlServer/source/server/index.js:9:9)
at tryCatch (/home/harrison/graphQlServer/source/node_modules/regenerator-runtime/runtime.js:45:40)
at Generator.invoke [as _invoke] (/home/harrison/graphQlServer/source/node_modules/regenerator-runtime/runtime.js:274:22)
(node:29648) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
这是我正在使用的MongoDB版本(通过社区版方法安装):
db version v4.2.6
git version: 20364840b8f1af16917e4c23c1b5f5efd8b352f8
OpenSSL version: OpenSSL 1.1.1 11 Sep 2018
allocator: tcmalloc
modules: none
build environment:
distmod: ubuntu1804
distarch: x86_64
target_arch: x86_64
答案 0 :(得分:2)
mongodb的连接字符串采用以下格式:
mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]
因此,您的情况将是:
mongodb://admin:password@127.0.0.1:27017/?authSource=admin
您可以详细了解here