MongoDB:管理员无权执行命令 [英] MongoDB: Not authorized on admin to execute command

查看:50
本文介绍了MongoDB:管理员无权执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 show dbs 命令时,我的 mongo shell 给了我这个错误.

<块引用>

未授权管理员执行命令

我尝试使用

创建用户

db.createUser({用户:站点用户管理员",pwd: "密码",角色:[{角色:userAdminAnyDatabase",数据库:admin"}]})

https://docs.mongodb.com/v2.6/tutorial/add-user-administrator/

但它仍然给我同样的错误:

<块引用>

无法添加用户:管理员无权执行命令.

我使用的是 Ubuntu 16.04mongoDB 版本是 3.4.9

解决方案

我认为您可以将角色用作 Ubuntu 的 root.试试下面的查询.

使用管理员db.createUser({用户:管理员",pwd: "密码",角色:[{角色:root",数据库:管理员"}]});出口;

您也可以授予用户读写权限

使用管理员db.grantRolesToUser("admin",["readWrite"])

编辑

由于上述方法无效,请尝试使用 auth 启动实例,如下所示

  1. 停止 mongod 实例
  2. 使用 $ mongod --auth
  3. 启动实例
  4. 现在启动mongo shell并创建用户

<块引用>

 使用管理员db.createUser({用户:管理员",pwd: "密码",角色:[{角色:root",数据库:admin"}]});出口;

  1. 现在授予权限

    <块引用>

     db.auth("admin", "password")db.grantRolesToUser("password", [ { role: "readWrite", db: "admin" } ])

  2. 现在检查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

  1. Stop the mongod instance
  2. Start the instance with $ mongod --auth
  3. Now start mongo shell and create user

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

  1. Now give permission

     db.auth("admin", "password")
     db.grantRolesToUser("password", [ { role: "readWrite", db: "admin" } ])
    

  2. Now check show users

这篇关于MongoDB:管理员无权执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆