Linux 未授权查询 mongodb 上的 admin.system.namespaces

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16704703/
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-08-06 22:59:15  来源:igfitidea点击:

Not authorized for query on admin.system.namespaces on mongodb

linuxmongodb

提问by user1208081

I start a new mongo instance, create a user, authorize it, but when I run "show collections", the system says that the id is not authorized. I do not know why?

我启动了一个新的 mongo 实例,创建了一个用户,对其进行了授权,但是当我运行“show collections”时,系统说该 ID 未被授权。我不知道为什么?

# mongo admin
MongoDB shell version: 2.4.3
connecting to: admin
Server has startup warnings:
Thu May 23 18:23:56.735 [initandlisten]
Thu May 23 18:23:56.735 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Thu May 23 18:23:56.735 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
Thu May 23 18:23:56.735 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
Thu May 23 18:23:56.735 [initandlisten]
> db = db.getSiblingDB("admin")
admin
> db.addUser({user:"sa",pwd:"sa",roles:["userAdminAnyDatabase"]})
{
        "user" : "sa",
        "pwd" : "75692b1d11c072c6c79332e248c4f699",
        "roles" : [
                "userAdminAnyDatabase"
        ],
        "_id" : ObjectId("519deedff788eb914bc429b5")
}
> show collections\
Thu May 23 18:26:50.103 JavaScript execution failed: SyntaxError: Unexpected token ILLEGAL
> show collections
Thu May 23 18:26:52.418 JavaScript execution failed: error: {
        "$err" : "not authorized for query on admin.system.namespaces",
        "code" : 16550
} at src/mongo/shell/query.js:L128
> db.auth("sa","sa")
1
> show collections
Thu May 23 18:27:22.307 JavaScript execution failed: error: {
        "$err" : "not authorized for query on admin.system.namespaces",
        "code" : 16550
} at src/mongo/shell/query.js:L128

回答by Robert Gannon

I would try granting the read role to the user. userAdminAnyDatabase grants the ability to administer users.

我会尝试向用户授予读取角色。userAdminAnyDatabase 授予管理用户的能力。

回答by Vityka

I had the same problem, but I found this tutorial and it helped me.

我遇到了同样的问题,但我找到了这个教程,它对我有帮助。

http://www.hacksparrow.com/mongodb-add-users-and-authenticate.html

http://www.hacksparrow.com/mongodb-add-users-and-authenticate.html

use:

用:

db.addUser('sa', 'sa')

instead of

代替

db.addUser({user:"sa",pwd:"sa",roles:["userAdminAnyDatabase"]})
{
        "user" : "sa",
        "pwd" : "75692b1d11c072c6c79332e248c4f699",
        "roles" : [
                "userAdminAnyDatabase"
        ],
        "_id" : ObjectId("519deedff788eb914bc429b5")
}

回答by Nick De Greek

I had the same problem and this is how I solved it:

我遇到了同样的问题,这就是我解决它的方法:

db = db.getSiblingDB('admin')
db.addUser( 
    { user: "mongoadmin", 
      pwd: "adminpass", 
      roles: ['clusterAdmin', 'userAdminAnyDatabase', 'readAnyDatabase'] } )

回答by hasib32

For MongoDB version 2.6 use:

对于 MongoDB 2.6 版,请使用:

db.createUser(
{
   user: "testUser"
   pwd: "password",
   roles: [{role: "readWrite", db:"yourdatabase"}]
})

See the docs

查看文档

回答by doom

As Robert says, admin users has only rights to admin, not to write in databases. So you have to create a custom user for your database. There's different ways. I have choose the dbOwner way.

正如罗伯特所说,管理员用户只有管理员权限,不能写入数据库。所以你必须为你的数据库创建一个自定义用户。有不同的方法。我选择了 dbOwner 方式。

(I use Ubuntu Server, mongo 2.6.3 and Robomongo)

(我使用 Ubuntu Server、mongo 2.6.3 和 Robomongo)

So to do this, fisrt create your admin user like mongo says :

因此,要做到这一点,首先要像 mongo 一样创建您的管理员用户:

type mongoin your linux shell

输入mongo你的 linux shell

and these command in the mongo shell :

以及 mongo shell 中的这些命令:

use admin
db.createUser({user:"mongoadmin",pwd:"chooseyouradminpassword",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
db.auth("mongoadmin","chooseyouradminpassword")
exit

edit the mongo conf file with :

使用以下命令编辑 mongo conf 文件:

nano /etc/mongod.conf

You can use vi if nano is not installed.

如果没有安装 nano,您可以使用 vi。

activate authentication by uncommented/adding these line auth=true

通过取消注释/添加这些行来激活身份验证 auth=true

if you want to use Robomongo from other machine change the line bind_ip=127.0.0.1by bind_ip=0.0.0.0(maybe you should add more protection in production).

如果你想行使用Robomongo从其他机器改变bind_ip=127.0.0.1bind_ip=0.0.0.0(也许你应该添加在生产更多的保护)。

type in linux shell :

在 linux shell 中输入:

service mongod restart
mongo

And in mongo shell :

在 mongo shell 中:

use admin
db.auth("mongoadmin","pwd:"chooseyouradminpassword")
use doomnewdatabase
db.createUser({user:"doom",pwd:"chooseyourdoompassword",customData:{desc:"Just me as I am"},roles : [{role:"dbOwner",db:"doomnewdatabase"}]})
db.auth("doom","chooseyourdoompassword")
show collections

(customData is not required).

(customData 不是必需的)。

If you want to try if it works, type this in the mongo shell :

如果您想尝试它是否有效,请在 mongo shell 中输入:

db.products.insert( { item: "card", qty: 15 } )
show collections
db.products.find()

Good luck ! Hope it will help you and others !

祝你好运 !希望它会帮助你和其他人!

I have search this informations for hours.

我已经搜索了几个小时的信息。

回答by Mohsen Shakiba

I solved it like so for mongoDB 2.6 + currently 3

我像这样解决了 mongoDB 2.6 + 目前 3

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

note that for the rolefiled instead of userAdminAnyDatabasewe use root

请注意,对于提交的角色而不是userAdminAnyDatabase,我们使用root