简体   繁体   English

无法在 Mongo 3.6.8 中添加管理员用户

[英]Can't add admin user in Mongo 3.6.8

I'm trying to add an admin user in Mongo, but I'm having trouble.我正在尝试在 Mongo 中添加管理员用户,但遇到了问题。

I already have auth = true commented out inside /etc/mongodb.conf .我已经在/etc/mongodb.conf注释掉了auth = true

When I run Mongo, I run these commands in order:当我运行 Mongo 时,我按顺序运行这些命令:

mongo

use admin

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

When I do so, I encounter the following error:当我这样做时,我遇到以下错误:

2020-10-11T04:50:50.714+0000 E QUERY    [thread1] Error: couldn't add user: there are
 no users authenticated :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.createUser@src/mongo/shell/db.js:1437:15
@(shell):1:1

I'm not sure exactly how to create an admin user if I have to log in as an admin user to do so.如果我必须以管理员用户身份登录,我不确定如何创建管理员用户。 Any help is appreciated!任何帮助表示赞赏!

You don't have to disable authentication.您不必禁用身份验证。 With authentication enabled you can still logon to your MonogDB without username/password.启用身份验证后,您仍然可以在没有用户名/密码的情况下登录到您的 MonogDB。 However, as soon as you create the first user with userAdmin or userAdminAnyDatabase role the access control becomes active and you have to authenticate.但是,一旦您创建第一个具有userAdminuserAdminAnyDatabase角色的用户,访问控制就会变为活动状态,您必须进行身份验证。

This behavior is called Localhost Exception此行为称为本地主机异常

Is this the first user to try to create?这是第一个尝试创建的用户吗? Perhaps another admin user already exist.也许另一个管理员用户已经存在。 See also How do I create a the first mongodb user with authorization enabled?另请参阅如何创建第一个启用授权的 mongodb 用户?

在导出旧数据库作为备份后,我只是放弃并重新开始使用新数据库。

For creating a user you will have to have a user in admin database first.要创建用户,您必须首先在admin数据库中有一个用户。 So while creating user you need to provide db name and role both.因此,在创建用户时,您需要同时提供数据库名称和角色。 Try creating user with below syntax.尝试使用以下语法创建用户。

use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: "password"
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
)

Reference-https://docs.mongodb.com/manual/tutorial/enable-authentication/参考 -https://docs.mongodb.com/manual/tutorial/enable-authentication/

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

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