更新时间:2023-11-11 20:50:10
我认为您可以将角色用作 Ubuntu 的 root.试试下面的查询.
使用管理员db.createUser({用户:管理员",pwd: "密码",角色:[{角色:root",数据库:管理员"}]});出口;
您也可以授予用户读写权限
使用管理员db.grantRolesToUser("admin",["readWrite"])
编辑
由于上述方法无效,请尝试使用 auth 启动实例,如下所示
$ mongod --auth
使用管理员db.createUser({用户:管理员",pwd: "密码",角色:[{角色:root",数据库:admin"}]});出口;
现在授予权限
db.auth("admin", "password")db.grantRolesToUser("password", [ { role: "readWrite", db: "admin" } ])
现在检查show users
My mongo shell is giving me this error when I am using show dbs
command.
not authorized on admin to execute command
I have tried to create a user using
db.createUser(
{
user: "siteUserAdmin",
pwd: "password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
as on https://docs.mongodb.com/v2.6/tutorial/add-user-administrator/
But it still gives me the same error:
Could not add user: not authorized on admin to execute command.
I am using Ubuntu 16.04 and mongoDB version is 3.4.9
I think you can use the role as root for Ubuntu. Try the below query.
use admin
db.createUser(
{
user: "admin",
pwd: "password",
roles: [ { role: "root", db: "admin" } ]
}
);
exit;
You can give read write permission to user as well
use admin
db.grantRolesToUser("admin",["readWrite"])
EDIT
Since the above didn't work try to start the instance with auth as below
$ mongod --auth
use admin db.createUser( { user: "admin", pwd: "password", roles: [ { role: "root", db: "admin" } ] } ); exit;
Now give permission
db.auth("admin", "password") db.grantRolesToUser("password", [ { role: "readWrite", db: "admin" } ])
Now check show users