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

Pra*_*mar 3 mongodb

我在谷歌云计算引擎中使用版本为3.4的mongoDB群集,实际上过去一周我的数据库被黑客攻击了,这就是为什么我考虑使用授权以便我可以避免这些类型的攻击.我们添加授权,我看到这篇文章如何对创建-MongoDB的复制集群,现在我已经加入了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
Run Code Online (Sandbox Code Playgroud)

我到处搜索但是没有找到任何关于我为什么会收到此错误的信息.

谁能帮助我,我怎么能解决这个错误.

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

辅助服务器配置

#!/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:
Run Code Online (Sandbox Code Playgroud)

仲裁服务器配置

#!/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:
Run Code Online (Sandbox Code Playgroud)

主服务器配置

#!/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:
Run Code Online (Sandbox Code Playgroud)

dat*_*uoc 13

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

security:
  authorization: disabled
Run Code Online (Sandbox Code Playgroud)

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

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