简体   繁体   English

创建具有读取本地数据库权限的mongo用户

[英]Create mongo user with right to read local database

I would like to create a mongo user who can read local database. 我想创建一个可以读取本地数据库的mongo用户。 I try to use command on local database: 我尝试在本地数据库上使用命令:

db.createUser(
  {     user: "readonlyuser", pwd: "loh8Ephi",
    roles: [ { role: "read", db: "local" } ]
})

But - it doesn't work. 但是-它不起作用。 I receive: 我收到:

connecting to: local
2015-12-21T14:08:07.904+0100 Error: couldn't add user: Cannot create users in the local database at src/mongo/shell/db.js:1054

I've tried to create that user against admin database: 我试图针对管理员数据库创建该用户:

> use admin
switched to db admin
> db.createUser(
...   {     user: "readonlyuser", pwd: "loh8Ephi", roles: [ { role: "read", db: "local" } ] })

Successfully added user: {
"user" : "readonlyuser",
"roles" : [
    {
        "role" : "read",
        "db" : "local"
    }
]

And now i try to connect: 现在我尝试连接:

undefine@machine:~$ mongo -u readonlyuser -p loh8Ephi local
MongoDB shell version: 2.6.11
connecting to: local
2015-12-21T15:35:19.190+0100 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:1292
exception: login failed

How to create readonly user who have access only to local database? 如何创建只能访问本地数据库的只读用户?

According the documentation , you cannot create users on the local database. 根据文档 ,您不能在本地数据库上创建用户。 Instead you can run your createUser query against the admin database: 相反,您可以对admin数据库运行createUser查询:

use admin
db.createUser(
 {     user: "readonlyuser", pwd: "loh8Ephi",
   roles: [ { role: "read", db: "local" } ]
})

Please note that you will have to authenticate against the admin database when connecting. 请注意,连接时必须对admin数据库进行身份验证。

I would do it like below 我会像下面这样

use admin
db.getSiblingDB("local").runCommand( { "createUser" : "mongoread", "pwd": "read0nly", "customData" : { "description": "mongo readonly user" }, "roles" : [ {"role" : "read","db" : "local"}] },{ w: "majority" , wtimeout: 5000 } );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM