MongoError: auth failed mongoose connection string

前端 未结 11 1530
一向
一向 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:58

    I installed MEAN at https://bitnami.com/stack/mean for windows 7 When install I make password is 123456

    Syntax make connect to mongodb with mongoose

    mongoose.connect("mongodb://[usr]:[pwd]@localhost:[port]/[db]",{auth:{authdb:"admin"}});
    

    If have no

    {auth:{authdb:"admin"}}
    

    You will get error message "MongoError: auth failed"

    Example: mongo-test/app.js

    var mongoose = require('mongoose');
    mongoose.connect('mongodb://root:123456@localhost/test',{auth:{authdb:"admin"}});
    mongoose.set('debug', true); // turn on debug
    
    0 讨论(0)
  • mongo mongodb://usr:psw@localhost:27017/dbname
    
    • Password should be alphanumeric only
    • User should be also available in db 'dbname' (Note : Even if user is super admin)

    With above changes it connected successfully.

    0 讨论(0)
  • 2020-12-29 08:00
    mongoose.connect("mongodb://[host]/[db]", { auth:{
    
        authdb: "admin",
        user: [username],
        password: [pw]
    
    }}).then(function(db){
    
        // do whatever you want
    
        mongoose.connection.close() // close db
    
    })
    
    0 讨论(0)
  • 2020-12-29 08:01

    You might want to do something like this...

    var opt = {
        user: config.username,
        pass: config.password,
        auth: {
            authdb: 'admin'
        }
    };
    var connection = mongoose.createConnection(config.database.host, 'mydatabase', config.database.port, opt);
    

    'authdb' option is the database you created the user under.

    0 讨论(0)
  • 2020-12-29 08:02

    Do you have a user set up for dbname? By default, no user is required to connect to the database unless you explicitly set one. If you haven't, you should just try to connect to mongodb://localhost:27017/dbname and see if you still get an error.

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