MongoError: auth failed mongoose connection string

前端 未结 11 1529
一向
一向 2020-12-29 07:10

I can connect to the DB through terminal, but getting this error using mongoose and gulp. mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:246 MongoError: auth

相关标签:
11条回答
  • 2020-12-29 07:36

    just make sure that your database is created. and also if your user is not added in the admin database, then make sure to add it by putting db.createUser( ... {user:'admin',pwd:'admin',roles:['root']} ... )

    0 讨论(0)
  • 2020-12-29 07:40
    mongoose.connect("mongodb://[usr]:[pwd]@localhost:[port]/[db]",{ authSource: 'admin', useNewUrlParser: true, useUnifiedTopology: true });
    

    I was getting same error. Resolved by adding authSource option to connect function solved the issue. see above code.

    0 讨论(0)
  • 2020-12-29 07:47

    just add ?authSource=yourDB&w=1 to end of db url

     mongoose.connect('mongodb://user:password@host/yourDB?authSource=yourDB&w=1')
    

    this work for me . &w=1 is important

    0 讨论(0)
  • 2020-12-29 07:47

    This worked for me for mongod --version = db version v3.6.13

    mongoose.connect('mongodb://localhost/expressapi', {
        auth: {
            authdb: "admin",
            user: "root",
            password: "root",
    
        }
    });
    
    0 讨论(0)
  • 2020-12-29 07:48

    There is many ways to make it work. This is what worked for me [mongoose v5.9.15] :

    mongoose.connect('mongodb://localhost:27017/', {
        auth: {
            user:'root',
            password:'example'
        },
        authSource:"admin",
        useUnifiedTopology: true,
        useNewUrlParser: true
    }
    
    0 讨论(0)
  • 2020-12-29 07:55

    I have found the solution hier, looks like when you create an user from the mongo shell, it makes SCRAM-SHA-1 instead of MongoDB-CR. So the solution to create a new user with MongoDB-CR authentication.

    MongoDB-CR Authentication failed

    0 讨论(0)
提交回复
热议问题