MongoDB - 管理员用户未授权

问题描述:

我正在尝试向我的 MongoDB 添加授权.
我正在使用 MongoDB 2.6.1 在 Linux 上完成所有这些操作.
我的 mongod.conf 文件是旧的兼容格式
(这是安装的方式).

I am trying to add authorization to my MongoDB.
I am doing all this on Linux with MongoDB 2.6.1.
My mongod.conf file is in the old compatibility format
(this is how it came with the installation).

1) 我按照 (3) 中的描述创建了管理员用户

1) I created admin user as described here in (3)

http://docs.mongodb.org/manual/tutorial/add-用户管理员/

2) 然后我通过取消注释这一行来编辑 mongod.conf

2) I then edited mongod.conf by uncommenting this line

auth = true

3) 最后,我重新启动了 mongod 服务,并尝试登录:

3) Finally I rebooted the mongod service and I tried to login with:

/usr/bin/mongo localhost:27017/admin -u sa -p pwd

4) 我可以连接,但它在连接时说这个.

4) I can connect but it says this upon connect.

MongoDB shell version: 2.6.1
connecting to: localhost:27017/admin
Welcome to the MongoDB shell!
The current date/time is: Thu May 29 2014 17:47:16 GMT-0400 (EDT)
Error while trying to show server startup warnings: not authorized on admin to execute command { getLog: "startupWarnings" }

5) 现在看来我创建的这个 sa 用户根本没有权限.

5) Now it seems this sa user I created has no permissions at all.

root@test02:~# mc
MongoDB shell version: 2.6.1
connecting to: localhost:27017/admin
Welcome to the MongoDB shell!
The current date/time is: Thu May 29 2014 17:57:03 GMT-0400 (EDT)
Error while trying to show server startup warnings: not authorized on admin to execute command { getLog: "startupWarnings" }
[admin] 2014-05-29 17:57:03.011 >>> use admin
switched to db admin
[admin] 2014-05-29 17:57:07.889 >>> show collections
2014-05-29T17:57:10.377-0400 error: {
        "$err" : "not authorized for query on admin.system.namespaces",
        "code" : 13
} at src/mongo/shell/query.js:131
[admin] 2014-05-29 17:57:10.378 >>> use test
switched to db test
[test] 2014-05-29 17:57:13.466 >>> show collections
2014-05-29T17:57:15.930-0400 error: {
        "$err" : "not authorized for query on test.system.namespaces",
        "code" : 13
} at src/mongo/shell/query.js:131
[test] 2014-05-29 17:57:15.931 >>>

有什么问题?我重复了整个过程 3 次
我想我按照 MongoDB 文档中的说明做了这一切.但它不起作用.
我期待这个 sa 用户被授权做任何事情,以便
然后他可以创建其他用户并授予他们更具体的权限.

What is the problem? I repeated this whole procedure 3 times and
I think I did it all as specified in the MongoDB docs. But it doesn't work.
I was expecting this sa user to be authorized to do anything so that
he can then create other users and give them more specific permissions.

我也遇到了同样的问题,在添加第一个管理员用户时将角色设置为 root 后一切正常.

I was also scratching my head around the same issue, and everything worked after I set the role to be root when adding the first admin user.

use admin
db.createUser(
  {
    user: 'admin',
    pwd: 'password',
    roles: [ { role: 'root', db: 'admin' } ]
  }
);
exit;

如果您已经创建了 admin 用户,您可以像这样更改角色:

If you have already created the admin user, you can change the role like this:

use admin;
db.grantRolesToUser('admin', [{ role: 'root', db: 'admin' }])

有关完整的身份验证设置参考,请参阅步骤经过数小时的互联网研究后编制而成.

For a complete authentication setting reference, see the steps I've compiled after hours of research over the internet.