MongoDB 创建副本集并启用身份验证

时间:2021-02-09 11:12:04

标签: mongodb

我有一个在 windows 上运行的 monogDB 副本集,我正在尝试向他们添加用户和身份验证。

use admin
db.createUser(
  {
    user: "admin",
    pwd: "password",
    roles: [ { role: "root", db: "admin" } ]
  }
);
exit;

mongo --port 27017 -u admin -p password --authenticationDatabase admin
use test
db.createUser(
    {
      user: "tester",
      pwd: "password",
      roles: [
         { role: "read", db: "test1" },
         { role: "read", db: "test2" },
         { role: "read", db: "test3" },
         { role: "readWrite", db: "test" }
      ]
    }
);

创建用户后,我将所有三个副本节点的 cfg 文件都修改为授权:启用。重新启动服务并尝试登录到服务器。 我可以与用户一起进入,但所有节点都将状态更改为“其他”,如果我将授权更改为未标记(# - 就像它一样),副本集工作正常,但我创建的用户不起作用。

有什么建议吗?

或者有人知道在启用身份验证的情况下创建副本集的实际步骤吗?

1 个答案:

答案 0 :(得分:1)

通常你会反过来做。首先启用身份验证,然后创建用户。通常所有用户都只在 admin 数据库中创建。

无论如何,关于您的问题:当您启用身份验证时,节点也必须在内部进行身份验证,即当一个副本集成员连接到另一个副本集成员时。

Internal/Membership Authentication

简单的方法是为此使用密钥文件。

首先创建密钥文件:

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

如果您没有 openSSL,可以从 OpenSSL for Windows 下载。然后将此密钥文件复制到每个副本集成员。

将此选项放入 MongoDB 配置文件:

security:
  authorization: enabled 
  keyFile: <path-to-keyfile>

重启MongoDB服务,它应该可以工作了。

另见Deploy Replica Set With Keyfile AuthenticationUpdate Replica Set to Keyfile AuthenticationUpdate Replica Set to Keyfile Authentication (No Downtime)