在mongdb群集上创建第一个管理员用户时获取错误“无法添加用户:未授权在管理员执行命令”

时间:2017-01-21 19:42:29

标签: mongodb

我在谷歌云计算引擎中使用版本为3.4的mongoDB群集,实际上过去一周我的数据库遭到黑客的攻击,这就是为什么我考虑使用授权以便我可以避免这些类型的攻击。现在要添加授权,我看到了这篇文章how-to-create-mongodb-replication-clusters,现在我已经在我的每个群集节点上添加了keyfile chmod 0600,但是现在当我尝试添加我的第一个{{1}时我得到以下错误

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:

6 个答案:

答案 0 :(得分:11)

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

npm start

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

security:
  authorization: disabled

答案 1 :(得分:2)

johnlowvale' answer是正确的,但

keyFile implies security.authorization.

来源:https://docs.mongodb.com/manual/reference/configuration-options/#security.keyFile

您必须禁用授权和密钥文件。

security:
  authorization: disabled
# keyFile: /opt/mongodb/keyfile

(代表不足或我刚刚在johnlowvale的评论中发表了评论)

答案 2 :(得分:0)

编辑vim /lib/systemd/system/mongod.service 删除--auth restarte

ExecStart = / usr / bin / mongod --quiet --auth --config /etc/mongod.conf

ExecStart = / usr / bin / mongod --quiet --config /etc/mongod.conf 使用管理员 db.createUser({用户: “RootAdmin” PWD “blahblah”,角色:[ “根”]})

答案 3 :(得分:0)

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

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

此后,您可以从外壳中添加所需的用户和角色。

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 然后重新启动服务器

答案 4 :(得分:0)

一旦连接到该第一个节点,就可以使用rs.initiate()初始化副本集。同样,此命令必须与mongod在同一主机上运行以使用localhost异常。

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

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

答案 5 :(得分:0)

如果在启用授权/安全性的情况下设置了新数据库,但未设置任何用户,则只能从本地主机连接到该数据库。在您的配置文件中,您应该将bind ip设置为127.0.0.1,我想为了确保您以正确的权限连接到它以创建新用户。

这就是Mongo M103课程

所说的

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