MongoDB:设置用户/密码以访问数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13850191/
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
MongoDB: set user/password to access to db
提问by f1nn
I'm trying to set password for mongodb to prevent access to db with empty login and pass (set by default).
我正在尝试为 mongodb 设置密码,以防止使用空登录名和密码(默认设置)访问数据库。
I'm statring mongo server:
我正在启动 mongo 服务器:
sudo ./mongod
Starting client:
启动客户端:
./mongo
Setting password:
设置密码:
use admin
db.addUser("root", "root")
exit
The output is:
输出是:
MongoDB shell version: 2.2.0
connecting to: test
> use admin
switched to db admin
> db.addUser("root", "root")
{
"user" : "root",
"readOnly" : false,
"pwd" : "2a8025f0885adad5a8ce0044070032b3",
"_id" : ObjectId("50c90b94e28c41a388104f64")
}
> exit
Hoever, wheh I try to auth with empty credentials (I use mViever admin UI), it still works. Otherwise, access with root/root is not avialable. What I'm doing wrong?
但是,当我尝试使用空凭据进行身份验证时(我使用 mViever 管理 UI),它仍然有效。否则,无法使用 root/root 访问。我做错了什么?
Also tried to start mongo server with -auth parameter, the same result:
还尝试使用 -auth 参数启动 mongo 服务器,结果相同:
./mongod -auth
UPD: After starting with -auth parameter can't login with any pass. Getting:
UPD:以 -auth 参数启动后,任何密码都无法登录。获得:
Thu Dec 13 03:27:38 uncaught exception: error {
"$err" : "unauthorized db:admin ns:admin.system.users lock type:1 client:127.0.0.1",
"code" : 10057
}
Update:I dont know what's goin on...
更新:我不知道发生了什么......
> db.auth("root","root");
1
> ^C
bye
It can login. Let's restart ./mongod --auth
and ./mongo
:
它可以登录。让我们重新启动./mongod --auth
并./mongo
:
MacBook-Pro-Ilya:bin ilyarusanen$ ./mongo
MongoDB shell version: 2.2.2
connecting to: test
> db.auth("root","root")
Error: { errmsg: "auth fails", ok: 0.0 }
0
> db.test.insert({"yeah":"2342"})
Fri Dec 14 08:52:05 uncaught exception: getlasterror failed: { "errmsg" : "need to login", "ok" : 0 }
> use admin
switched to db admin
> db.addUser("root","root")
Fri Dec 14 08:52:14 uncaught exception: error {
"$err" : "unauthorized db:admin ns:admin.system.users lock type:1 client:127.0.0.1",
"code" : 10057
}
> db.auth("root","root")
1
Why at first it can login? Why after restarting mongo is not able to login? And why after FAILED attempt to addUser, it becomes able to login? Thanks.
为什么一开始可以登录?为什么重启mongo后无法登录?为什么在尝试添加用户失败后,它变得能够登录?谢谢。
UPDATE2: MongoHub seems to auth ok. However, from NodeJS I still can't login: I use such code:
UPDATE2:MongoHub 似乎可以验证。但是,从 NodeJS 我仍然无法登录:我使用这样的代码:
mongo_db.open(function(err,data){
if(data){
data.authenticate("root", "root",function(err2,data2){
if(data2){
console.log("Database opened");
}
else{
console.log(err2);
}
});
} else {
console.log(err);
}
});
And I get:
我得到:
{ [MongoError: auth fails] name: 'MongoError', errmsg: 'auth fails', ok: 0 }
But mention, MongoHub with same credentials works fine.
但请注意,具有相同凭据的 MongoHub 工作正常。
采纳答案by Andre de Frere
From your comment you mention that you are using mViewer. Version 0.9.1 of mViewer does not support authentication. According to this issueon the mViewer GitHub, this is resolved in version 0.9.2, which was targeted for release in Oct.
从您的评论中您提到您正在使用 mViewer。mViewer 0.9.1 版本不支持身份验证。根据mViewer GitHub 上的这个问题,这个问题在 0.9.2 版本中得到解决,该版本将于 10 月发布。
Before starting the node with authentication, log on to the node and add a user. Then start the node with --auth
and connect to the shell without mViewer.
在使用身份验证启动节点之前,先登录节点并添加用户。然后启动节点--auth
并连接到没有 mViewer 的外壳。
At this point you can connect to the admin database and authenticate your admin user:
此时,您可以连接到 admin 数据库并验证您的 admin 用户:
use admin
db.auth('root', 'root')
Since you set up an admin user, which will have access to all the databases, you need to authenticate against the admin database. Once you have done this you will have access to all the databases. You will also be able to create new users on any database, or create new read only users for all the databases.
由于您设置了一个可以访问所有数据库的 admin 用户,因此您需要对 admin 数据库进行身份验证。完成此操作后,您将可以访问所有数据库。您还可以在任何数据库上创建新用户,或为所有数据库创建新的只读用户。
If you create a new user that has access to only one database, that user would need to use
that database and db.auth(name, pass)
against it.
如果您创建一个只能访问一个数据库的新用户,该用户将需要访问use
该数据库并db.auth(name, pass)
针对它。
If you create a new user that has read only access to all databases, they would use admin
and then db.auth(name, pass)
to gain their read only access to all databases
如果您创建具有读取所有数据库只访问一个新的用户,他们会use admin
再db.auth(name, pass)
得到他们只读到所有数据库的访问
You can find more information on setting up authentication hereand more information about setting up users here
你可以找到设置身份验证的详细信息在这里和更多信息有关设置用户在这里
Note: When you start a node without--auth
then no authentication is enabled. This means you can connect with the shell and db.auth('root','root')
but it won't do anything as far as access is concerned. MongoDB will not deny access to the databases without --auth
command line option (--keyFile
in sharded setups or replica sets)
注意:当您启动一个节点时,--auth
则不会启用任何身份验证。这意味着您可以与 shell 连接,db.auth('root','root')
但就访问而言,它不会做任何事情。MongoDB 不会拒绝在没有--auth
命令行选项的情况下访问数据库(--keyFile
在分片设置或副本集中)