My current Mongoose connection method is failing to connect. I'm able to connect to my database in the shell without a problem:
mongo --username 'admin' --password 'mypassword' --authenticationDatabase 'admin'
This is the output it provides:
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
However, when I try to use my previously functional Mongoose connection method, it's stalling and not actually connecting. Here's the connection method that I'm using in 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;
}
Eventually, it gives me this error (even though I'm using the same credentials that I'm using from the command line):
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)
Here's the MongoDB version that I'm using (installed via the community edition method):
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
Connection string fro mongodb is in this format:
mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]
So in your case it will be:
mongodb://admin:password@127.0.0.1:27017/?authSource=admin
You can read more about it here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.