mongodb MongoError: auth failed mongoose 连接字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30105823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:25:34  来源:igfitidea点击:

MongoError: auth failed mongoose connection string

mongodbmongooseconnection-string

提问by Sunkat

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 failed

我可以通过终端连接到数据库,但是使用 mongoose 和 gulp 出现此错误。mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:246 MongoError: auth failed

My connection string is:

我的连接字符串是:

mongodb://usr:psw@localhost:27017/dbname

Any idea what it can be?

知道它可以是什么吗?

回答by Thien Nguyen

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

我在Windows 7 的https://bitnami.com/stack/mean 上安装了 MEAN安装时我设置的密码是 123456

Syntax make connect to mongodb with mongoose

语法 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"

您将收到错误消息“MongoError: auth failed”

Example:mongo-test/app.js

示例: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

回答by ali karimi

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

只需添加?authSource=yourDB&w=1到 db url 的末尾

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

this work for me . &w=1is important

这对我有用。&w=1很重要

回答by Leon

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.

'authdb' 选项是您在其下创建用户的数据库。

回答by Kamagatos

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/dbnameand see if you still get an error.

您是否为 dbname 设置了用户?默认情况下,除非您明确设置,否则不需要用户连接到数据库。如果没有,您应该尝试连接mongodb://localhost:27017/dbname并查看是否仍然出现错误。

回答by anniex

mongoose.connect("mongodb://[host]/[db]", { auth:{

    authdb: "admin",
    user: [username],
    password: [pw]

}}).then(function(db){

    // do whatever you want

    mongoose.connection.close() // close db

})

回答by user3531155

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.

我找到了解决方案,看起来当您从 mongo shell 创建用户时,它会生成 SCRAM-SHA-1 而不是 MongoDB-CR。因此,使用 MongoDB-CR 身份验证创建新用户的解决方案。

MongoDB-CR Authentication failed

MongoDB-CR 身份验证失败

回答by Apoorv Bankey

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']} ... )

只需确保您的数据库已创建。此外,如果您的用户未添加到 admin 数据库中,请确保通过放置 db.createUser( ... {user:'admin',pwd:'admin',roles:['root']} 来添加它。 ..)

回答by Madhuraman Ghimire

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

这对我有用 mongod --version = db version v3.6.13

mongoose.connect('mongodb://localhost/expressapi', {
    auth: {
        authdb: "admin",
        user: "root",
        password: "root",

    }
});

回答by Sandeep PC

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)
  • 密码只能是字母数字
  • 用户也应该在 db 'dbname' 中可用(注意:即使用户是超级管理员)

With above changes it connected successfully.

通过上述更改,它成功连接。

回答by akheron

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

有很多方法可以让它发挥作用。这对我有用 [猫鼬 v5.9.15]:

mongoose.connect('mongodb://localhost:27017/', {
    auth: {
        user:'root',
        password:'example'
    },
    authSource:"admin",
    useUnifiedTopology: true,
    useNewUrlParser: true
}