在创建MongoDB集群的第一个管理员用户时,出现错误“无法添加用户:未被授权在管理员上执行命令”。

7
我正在使用版本为3.4的mongoDB集群在谷歌云计算引擎上,实际上,上周我的数据库遭到了黑客攻击,因此我考虑使用授权来避免这类攻击。现在,为了添加授权,我看到了这篇文章how-to-create-mongodb-replication-clusters,现在我已经在我的集群节点上添加了一个keyfilechmod 0600,但是当我尝试添加第一个admin user时,我遇到了以下错误。
use admin
switched to db admin
rs0:PRIMARY> db.createUser({user: "RootAdmin", pwd: "password123", roles: [ { role: "root", db: "admin" } ]});
2017-01-21T18:19:09.814+0000 E QUERY    [main] Error: couldn't add user: not authorized on admin to execute comm
and { createUser: "RootAdmin", pwd: "xxx", roles: [ { role: "root", db: "admin" } ], digestPassword: false, writ
eConcern: { w: "majority", wtimeout: 300000.0 } } :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.createUser@src/mongo/shell/db.js:1290:15
@(shell):1:1

我已经到处搜索,但没有找到为什么会出现这个错误的原因。

请问有人可以帮我解决这个错误吗?

更新 下面给出了每个实例的配置文件

辅助服务器配置

#!/bin/bash
# mongod.conf
# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: false
   #engine:
  mmapv1:
    smallFiles: true
#  wiredTiger:
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0
replication:
  replSetName: rs0
#processManagement:
security:
  authorization: disabled
  keyFile: /opt/mongodb/keyfile
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:

仲裁服务器配置

#!/bin/bash
# mongod.conf
# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
  dbPath: /mnt/mongodb/db
  journal:
    enabled: true
   #engine:
  #mmapv1:
    #smallFiles: true
#  wiredTiger:
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /mnt/mongodb/log/mongodb.log
# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0
replication:
  replSetName: rs0
#processManagement:
security:
  authorization: disabled
  keyFile: /opt/mongodb/keyfile
#operationProfiling:

#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:

主服务器配置

#!/bin/bash
# mongod.conf
# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
  dbPath: /mnt/mongodb/db
  journal:
    enabled: true
   #engine:
  #mmapv1:
    #smallFiles: true
#  wiredTiger:
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /mnt/mongodb/log/mongodb.log
# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0
replication:
  replSetName: rs0
#processManagement:
security:
  authorization: disabled
  keyFile: /opt/mongodb/keyfile
#operationProfiling:

#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:

1
仅作提示,您在问题中显示了密码。 - Dee
1
你解决了这个问题吗?如果是,请更新或回答自己。你检查过日志文件了吗 - 里面可能有线索。 - Nic Cottrell
6个回答

19

在创建此类管理员用户之前,您需要更改 mongod.conf 文件以禁用授权。

security:
  authorization: disabled

之后,重新启动mongod服务并打开mongodb shell创建管理员用户

use admin
db.createUser({user:"RootAdmin",pwd:"blahblah",roles:["root"]})

创建用户后,请记得重新启用授权。


1
我已经添加了 authorization: disabled 但仍然遇到相同的错误。我已经更新了所有实例的配置文件。请问您能否告诉我在mongoDB副本集中添加身份验证的源URL或整个过程,感谢您帮助我解决这个问题。 - Prakash Kumar
很奇怪,当授权被禁用时,你可以做任何事情。 - Dee
你能告诉我你整个身份验证过程中使用的源(URL)吗? - Prakash Kumar

4

2
和 OP 一样遇到了同样的问题,keyFile implies security.authorization. -- 这就是整个情况的原因。感谢这一小行文字解决了我所有的问题。 - emmdee
1
是的,这是正确的,你应该同时注释keyFile!! - whoami - fakeFaceTrueSoul

3

当你连接到第一个节点后,你可以使用rs.initiate()来初始化副本集。需要注意的是,这个命令必须在与mongod相同的主机上运行,并使用localhost例外。

我们可以使用以下命令创建管理员用户:

rs.initiate()
use admin
db.createUser({
  user: "admin",
  pwd: "pass",
  roles: [
    {role: "root", db: "admin"}
  ]
})

rs.initiate() 救了我。 - cyrf

1
编辑 /lib/systemd/system/mongod.service,使用vim命令。
remove --auth 
restart

#ExecStart=/usr/bin/mongod --quiet --auth   --config /etc/mongod.conf
ExecStart=/usr/bin/mongod --quiet  --config /etc/mongod.conf
use admin
db.createUser({user:"RootAdmin",pwd:"blahblah",roles:["root"]})

0

要创建新用户,您需要先在 /etc/mongod.conf 中禁用安全性。

// security: // authorization: enabled 然后重新启动 Mongodb 服务器 sudo service mongo restart

之后,您可以从 shell 添加所需的用户和角色。

db.createUser({ user: 'test_user', pwd: 'test', roles: [ { role: "userAdmin", db: "test" }, { role: "dbAdmin", db: "test" }, { role: "readWrite", db: "test" } ] })

要启用经过身份验证的连接,请取消注释 /etc/mongod.conf 中的该行。

security: authorization: enabled 然后再次重启服务器。


0

当启用授权/安全性但未设置用户的新数据库建立时,您只能从本地主机连接到它。在配置文件中,您应该将绑定IP设置为127.0.0.1,以确保您使用正确的授权连接到它来创建新用户。

这是Mongo课程M103中的内容。

默认情况下,强制执行身份验证但没有配置用户的mongod仅允许通过本地主机进行连接。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接