how to authenticate mongoose connection mongodb in node.js

前端 未结 7 2061
没有蜡笔的小新
没有蜡笔的小新 2021-02-20 04:38

I have created mongodb user with command

use admin
db.createUser(
    {
      user: \"superuser\",
      pwd: \"12345678\",
      roles: [ \"root\" ]
    }
)
         


        
7条回答
  •  悲&欢浪女
    2021-02-20 05:40

    You must declare the authSource parameter in your connection string in order to specify the name of the database that contains your user's credentials:

    var options = {
      user: "superuser",
      pass: "12345678"
    };
    
    var mongooseConnectionString = 'mongodb://localhost/twitter-mongo?authSource=admin';
    

    Note: for users of Mongoose 4.x, you may want to also include useMongoClient: true in your options object. This silences the Please authenticate using MongoClient.connect with auth credentials and open() is deprecated error messages.

提交回复
热议问题