1

mongodb.conf我有 4 个 mongodb 实例正在运行(副本集),每个实例都有以下文件:

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /root/mongodata/log/mongod.log

# Where and how to store data.
storage:
  dbPath: /root/mongodata/db1 # also have db2 and so on for rest of the instances
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /root/mongodata/db1/mongod.pid  # location of pidfile, different for 4 instances

# network interfaces
net:
  port: 30000 #port different for 4 different instances
  bindIp: 12.123.321.432(example ip, same for all 4 .conf files) 

# security  
security:
  KeyFile: /path to my keyfile location

#  authorization: enabled

#operationProfiling:

replication:
  replSetName: testReplica #have this same for all 4


#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

我还为内部身份验证创建了一个密钥文件,如下所示:

openssl rand -base64 756 > <path-to-keyfile>
chmod 400 <path-to-keyfile>

在所有实例运行后,我打开了 mongoShell,如下所示:

mongo --host 12.123.321.432 --port 30000

我可以打开 shell,但是当我尝试创建用户时,出现以下异常:

2016-12-22T20:55:38.396-0500 E QUERY    [thread1] Error: couldn't add user: not authorized on test to execute command { createUser: "root", pwd: "xxx", roles: [ { role: "root", db: "admin" } ], digestPassword: false, writeConcern: { w: "majority", wtimeout: 30000.0 } } :
_getErrorWithCode@src/mongo/shell/utils.js:23:13
DB.prototype.createUser@src/mongo/shell/db.js:1230:11
@(shell):1:1

我尝试切换到管理数据库,但仍然说未经授权,我还尝试运行 rs.initiate() 命令来定义主数据库和辅助数据库,说未经授权。即使我开始mongod使用身份验证禁用内部身份验证,我也会阅读keyfile将强制基于角色的身份验证。我在这里缺少什么,我将如何解决它?提前致谢。

4

1 回答 1

1

错误消息显示您没有执行该命令的权限。

在.conf文件中设置keyfile参数后,mongo 需要您使用 auth 用户登录。

所以如果你没有root用户。

  1. 在没有身份验证配置的情况下启动 mongod(不要设置密钥文件和禁用 security.authorization
  2. 使用 root 角色创建使用
db.createUser({user:"root",pwd:"rootpassword",roles:[{role:"root",db:"admin"}]})

如果你有 root 用户,你应该使用db.auth()或以 root 权限登录 mongo 来执行rs.conf()命令。

mongo --port portnumber -u root -p --authenticationDatabase admin

或使用命令mongo --port portnumber登录后

db.auth("root", "rootpassword")

附言。KeyFile 用于副本集内部通信安全。

于 2016-12-23T03:15:14.657 回答