【问题标题】:Authenticate mongodb replica set验证 mongodb 副本集
【发布时间】:2021-03-11 17:54:39
【问题描述】:

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

# 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 的内部身份验证将强制基于角色的身份验证。我在这里缺少什么,我将如何解决它?提前致谢。

【问题讨论】:

    标签: mongodb authentication mongodb-internal-auth


    【解决方案1】:

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

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

    所以如果你没有root用户。

    1. 在没有身份验证配置的情况下启动 mongod(不要设置 keyfile 并禁用 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")
    

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

    【讨论】:

    • 您几乎是正确的,但是对于副本集,诀窍是从空的 db 文件夹开始,并且在 .conf 文件中您不应该提供'keyFile 参数。只要您输入keyFile,内部身份验证+基于角色的访问就会被强制执行。所以在没有keyfile 和创建必要的用户的情况下开始,然后在keyFile 添加到.conf 文件中重新启动。注意:当您提供keyFile 参数时,您不需要使用--auth 来启用身份验证。请更新您的答案,以便我标记为正确答案。谢谢!
    • @Gurkha 是的,没错,答案已更新,希望对您有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    相关资源
    最近更新 更多