MongoDB – pipe理员用户未经授权

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

1)我创build了admin用户,如(3)

http://docs.mongodb.org/manual/tutorial/add-user-administrator/

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

auth = true

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

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

4)我可以连接,但它说连接。

 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)现在看来,我创build的sa用户根本没有权限。

 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文档中指定的。 但它不起作用。
我期待这个用户被授权做这样的事情
他然后可以创build其他用户,并给他们更具体的权限。

我也在同一个问题上摸不着头脑,而且在添加第一个pipe理员用户时,我将angular色设置为root。

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

有关完整的身份validation设置参考,请参阅在互联网上进行数小时研究后编制的步骤 。

这有点令人困惑 – 我相信你将需要给自己readWrite查询数据库。 具有dbadmin或useradmin的用户可以pipe理数据库(包括授予您其他权限),但不能执行查询或写入数据。

所以给自己readWrite,你应该罚款 –

http://docs.mongodb.org/manual/reference/built-in-roles/#readWrite

也许一个如何改变当前用户的简单例子对于某个人会有所帮助。 这是我真正想要的。

遵循@JohnPetrone的build议,我用grantRolesToUser将readWriteangular色添加到了我的pipe理员用户

 > use admin > db.grantRolesToUser("admin",["readWrite"]) > show collections system.users system.version 

你可以尝试:使用–authenticationDatabase标志帮助。

 mongo --port 27017 -u "admin" -p "password" --authenticationDatabase "admin" 

这可能是因为你没有在mongodb.conf中设置noAuth = true

 # Turn on/off security. Off is currently the default noauth = true #auth = true 

设置完成后,重新启动服务

服务mongod重启

我知道这个答案是在这个线程真的很晚,但我希望你看看。

您得到该错误的原因是基于您授予用户的特定angular色(您现在已经收集到的),并且是给该用户angular色root可以解决您的问题,但是您必须首先了解这些angular色在授予他们给用户。

在教程中,您授予用户userAdminAnyDatabaseangular色,该angular色基本上使用户能够pipe理所有数据库的用户。 你想要对你的用户做什么是在它的angular色定义之外。

rootangular色具有包含在其定义中的readWriteAnyDatabase ,以及使其成为超级用户的readWriteAnyDatabasedbAdminAnyDatabase和其他angular色(基本上是因为您可以用它做任何事情)。

您可以查看angular色定义,查看您需要为用户完成某些任务所需的angular色。 https://docs.mongodb.com/manual/reference/built-in-roles/它是不可取的,使所有的用户超级;:)

我在Windows环境下遇到类似的问题:我已经安装了Bitnami DreamFactory,并且安装了另一个在系统启动时启动的MongoDb。 我正在运行我的MongoDbService(这是开始没有任何错误),但我注意到我失去了很多时间后,我实际上连接在Bitnami的MongoDb服务。 请看看你的服务器上没有运行mongoDB的另一个实例。

祝你好运!

另外,请注意,如果您的mongo shell客户端无法正确连接到mongod实例,则可能会收到“拒绝权限”错误。

确保您的客户端通过检查连接端口来打开连接,但也确保您在mongod中使用的端口未被使用。 您可以通过在shell和进程中使用--port <port>参数来设置不同的端口。

我有同样的问题。 在运行mongod时,我拿出了–auth,并提供了一个临时解决scheme。