代码之家  ›  专栏  ›  技术社区  ›  Harrison Cramer

Mongoose未连接到MongoDB 4.2

  •  0
  • Harrison Cramer  · 技术社区  · 4 年前

    我当前的Mongoose连接方法无法连接。我能够毫无问题地连接到shell中的数据库:

    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连接方法时,它出现了停滞,实际上并没有连接。以下是我在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
    
    1 回复  |  直到 4 年前
        1
  •  2
  •   Aritra Chakraborty    4 年前

    mongodb的连接字符串格式如下:

    mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]
    

    因此,在您的情况下,它将是:

    mongodb://admin:[email protected]:27017/?authSource=admin
    

    你可以阅读更多关于它的信息 here