node.js 无法连接到 mongolab,获取 MongoError: auth failed
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30924859/
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
Unable to connect to mongolab, Getting MongoError: auth failed
提问by mkuniyil
I have recently created an account in mongoLab.When I am trying to connect to the database using the below statement.
我最近在 mongoLab 中创建了一个帐户。当我尝试使用以下语句连接到数据库时。
var mongoose = require('mongoose');
mongoose.connect('mongodb://mk:[email protected]:47742/mkdb');
I'm always getting the following error
我总是收到以下错误
MongoError: auth failed
at Function.MongoError.create (/Users/a042292/Desktop/start/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:31:11)
at /Users/a042292/Desktop/start/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:793:66
at Callbacks.emit (/Users/a042292/Desktop/start/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:94:3)
at null.messageHandler (/Users/a042292/Desktop/start/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:235:23)
at Socket.<anonymous> (/Users/a042292/Desktop/start/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:259:22)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:538:20)
回答by Joseph
Make sure you are using the database usernameand passwordnot the account usernameand passwordfrom Mlab.
确保您使用的是数据库username而password不是帐户username和password来自 Mlab。
In MLab, formerly MongoLab, do the following
在 MLab(以前称为 MongoLab)中,执行以下操作
- Navigate to
Users Add Database User- Choose your username and password
- 导航
Users Add Database User- 选择您的用户名和密码
Now you can test this on the shell with
mongo ds061374.mlab.com:61374/yourdb -u <dbuser> -p <dbpassword>
现在你可以在 shell 上测试这个
mongo ds061374.mlab.com:61374/yourdb -u <dbuser> -p <dbpassword>
回答by gtsouk
Mongolab upgraded their 2.6.x databases to 3.0.x. Unfortunately mongo3 has a different authentication mechanism so old clients are not compatible.
Mongolab 将他们的 2.6.x 数据库升级到 3.0.x。不幸的是 mongo3 具有不同的身份验证机制,因此旧客户端不兼容。
Mongoose is using the native mongo driver so you have to upgrade it. This is usually done by upgrading your local mongo installation.
Mongoose 正在使用本机 mongo 驱动程序,因此您必须对其进行升级。这通常是通过升级本地 mongo 安装来完成的。
For those using mongojs, upgrade to the latest version and add the authMechanism:'ScramSHA1'parameter in the options object upon connection:
对于使用 mongojs 的用户,请升级到最新版本并authMechanism:'ScramSHA1'在连接时在 options 对象中添加参数:
db = mongojs('mongodb://username:[email protected]:32132/mydb', ["mycollection"], {authMechanism: 'ScramSHA1'});
回答by Oded Regev
For me the solution was:
对我来说,解决方案是:
$ npm install --save --save-exact [email protected]
According to: Heroku app crashes after MongoDB updated to 3.0
回答by Rumesh Madushanka
In here we have to know that mLab username and Password are not the username and password for Our database too...there for we have to check whether we have used correct username and password for connection String
在这里我们必须知道mLab用户名和密码也不是我们数据库的用户名和密码......因为我们必须检查我们是否使用了正确的用户名和密码来连接字符串
we can create Database user account in here---->>
我们可以在这里创建数据库用户帐户---->>
My connection const as follows
我的连接常量如下
const db ="mongodb://<My database username>:<my database password>.mlab.com:39648/videoplayer"
回答by sannimichaelse
just add ?authSource=yourDB&w=1 to 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=1 is important
mongoose.connect('mongodb://user:password@host/yourDB?authSource=yourDB&w=1') 这对我有用。&w=1 很重要
e.g
例如
MONGO_URI='mongodb://kahn:[email protected]:13402/ecommerce?authSource=ecommerce&w=1';
MONGO_URI='mongodb://kahn:[email protected]:13402/ecommerce?authSource=ecommerce&w=1';
https://github.com/Automattic/mongoose/issues/4587
https://github.com/Automattic/mongoose/issues/4587
This saved my life
这救了我的命
回答by Al-Maamari Tareq
1- make sure the db is up and running. 2- dont forget to create the db user to have access credentials.
1-确保数据库已启动并正在运行。2- 不要忘记创建 db 用户以获得访问凭据。
Wish that will help you !
希望对你有帮助!
回答by Mushirih
I was getting this error while using an older version of mongoose(version 3.8.10).After upgrading to the latest release(version 5.0.10) the error dissapeared and a connection was made.
我在使用旧版本的猫鼬(3.8.10 版)时遇到此错误。升级到最新版本(5.0.10 版)后,错误消失并建立了连接。
Just run npm install [email protected] --save ....But replace the version with the most recent release,
只需运行 npm install [email protected] --save ....但用最新版本替换版本,
回答by Pooja S Arkasali
Make sure you are using proper db username and password.
确保您使用正确的数据库用户名和密码。
If you are trying to connect to db through your code and your username and password has any speacial characters like '@','$' etc , make sure you encode your URI using encodeURIComponent()funtion
如果您尝试通过代码连接到 db,并且您的用户名和密码包含任何特殊字符,例如 '@'、'$' 等,请确保使用encodeURIComponent()函数对您的 URI 进行编码
example : "localhost://pooja:"+encodeURIComponent('pooja@123')+"/trymynewdb", then use the enocded uri to connect to db.
例如:"localhost://pooja:"+encodeURIComponent('pooja@123')+"/trymynewdb",然后使用编码的 uri 连接到数据库。
回答by Jero Dungog
If your password has special characters it would be best to check the url encoding value of the special character present here: url encoding list
如果您的密码包含特殊字符,最好检查此处存在的特殊字符的 url 编码值:url encoding list
But I highly suggest you verify your data being sent first before attempting to connect. One way to verify it is to console.log the data being sent. Example:
但我强烈建议您在尝试连接之前先验证您的数据是否被发送。验证它的一种方法是 console.log 正在发送的数据。例子:
console.log(process.env.MONGO_ATLAS_PW);
回答by umang-malhotra
You can try connecting via mshell, I had this similar problem while connecting using mongoose even when I had given it database user name and it's correct password.
您可以尝试通过 mshell 连接,我在使用 mongoose 连接时遇到了类似的问题,即使我给了它数据库用户名和正确的密码。
just type following command in the terminal
只需在终端中输入以下命令
mongo ds239412.mlab.com:39412/videoplayer11 -u dbuser -p dbpassword (command will be different for you, see here).
mongo ds239412.mlab.com:39412/videoplayer11 -u dbuser -p dbpassword(命令对您来说会有所不同,请参见此处)。
and remove the code in your model file where you were connecting via mongoose.
并删除您通过猫鼬连接的模型文件中的代码。
Worked for me. Happy faces.
对我来说有效。幸福的脸。

