gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 10:00:33 25 4
gpt4 key购买 nike

我想创建一个可以读取本地数据库的 mongo 用户。我尝试在本地数据库上使用命令:

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

但是 - 它不起作用。我收到:

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

我已尝试针对管理数据库创建该用户:

> 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"
}
]

现在我尝试连接:

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

如何创建只能访问本地数据库的只读用户?

最佳答案

根据documentation ,您不能在本地数据库上创建用户。相反,您可以针对 admin 数据库运行 createUser 查询:

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

请注意,连接时您必须对 admin 数据库进行身份验证。

关于mongodb - 创建具有读取本地数据库权限的mongo用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34397157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com