简体   繁体   English

如何为副本集启动身份验证 - MongoDB

[英]How to initiate authentication for replica set - MongoDB

There is a replica set without authentication. 没有身份验证的副本集。 I want to create its authentication for first time. 我想第一次创建它的身份验证。

I do as following: 我做如下:

1- create [administrator user][1]
2- restarting all member with option `auth=true`
3- login as aadministrator to one member of replica set
4- trying to create user "db.addUser(...)"

but when I want to create user, it throw exception of couldn't add user: not master at src/mongo/shell/db.js:128 但是当我想创建用户时,它抛出了couldn't add user: not master at src/mongo/shell/db.js:128异常couldn't add user: not master at src/mongo/shell/db.js:128

What should I do? 我该怎么办? is it possible initiate security in existing replica set Or I should, remove replica set and rebuild it, after setting authentication. 是否可以在现有副本集中启动安全性或者我应该在设置身份验证后删除副本集并重建它。

If replica set already exists, you need to find the primary node, add a user with "root" role, and for each database add a user with admin/writeAndRead/read role, and/or add an admin user for all databases. 如果副本集已存在,则需要查找主节点,添加具有“root”角色的用户,并为每个数据库添加具有admin / writeAndRead / read角色的用户,和/或为所有数据库添加admin用户。

use admin

db.createUser({ user: "rootUser", pwd: "rootPass", roles: [ { role: "root", db: "admin" } ] })

db.createUser({ user: "admin", pwd: "adminPass", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })

use otherDb

db.createUser({ user: "rwUser", pwd: "rwUserPass", roles: [{ role: "readWrite", db: "otherDb" }] })

Wait until sync all replica nodes. 等到同步所有副本节点。 Set auth=yes on each mongod.conf file (this will force each client to use user/pass). 在每个mongod.conf文件上设置auth = yes(这将强制每个客户端使用user / pass)。

If you want (not required), to add keyFile to enforce extra security steps between all replica set, you can create this file, copy between each node and enable keyFile option inside each mongod.conf file, but this is only to force replica set nodes to know a secret between them and start talking, not for client applications. 如果您需要(不需要),添加keyFile以在所有副本集之间强制执行额外的安全步骤,您可以创建此文件,在每个节点之间复制并在每个mongod.conf文件中启用keyFile选项,但这只是强制副本集节点知道它们之间的秘密并开始说话,而不是客户端应用程序。

Finally restart the primary node wait for new primary election and continue restarting all nodes inside replica set. 最后重启主节点等待新的主要选举并继续重新启动副本集内的所有节点。

Couple of useful links for create secret key file http://docs.mongodb.org/v2.6/tutorial/deploy-replica-set-with-auth/#create-the-key-file-to-be-used-by-each-member-of-the-replica-set and more details for the mongodb v2.6 version http://docs.mongodb.org/v2.6/tutorial/deploy-replica-set-with-auth/#create-the-key-file-to-be-used-by-each-member-of-the-replica-set 几个有用的链接用于创建密钥文件http://docs.mongodb.org/v2.6/tutorial/deploy-replica-set-with-auth/#create-the-key-file-to-be-used- by-each-of-the-replica-set和mongodb v2.6版本的更多细节http://docs.mongodb.org/v2.6/tutorial/deploy-replica-set-with-auth/#创建最密钥文件将要使用的逐各部件的最副本集

Since you are configuring a replicaSet, I believe you need to use the keyFile option rather than auth=Yes. 由于您正在配置replicaSet,我认为您需要使用keyFile选项而不是auth = Yes。 This will allow the nodes in the replicaSet to communicate with eachother once authentication is enabled. 一旦启用了身份验证,这将允许replicaSet中的节点相互通信。

Check this doc. 检查此文档。 http://docs.mongodb.org/manual/tutorial/enable-authentication http://docs.mongodb.org/manual/tutorial/enable-authentication

though replica set exists, it is not a master or master has not set. 虽然副本集存在,但它不是主服务器或主服务器没有设置。 you might haven't init replica set yet. 你可能还没有init副本集。

https://docs.mongodb.com/manual/tutorial/deploy-replica-set/ https://docs.mongodb.com/manual/tutorial/deploy-replica-set/

> rs.initiate()
> rs.add("secondary-host:27017")
> rs.add("more-hosts-if-exist:27017")

and then you could create user. 然后你可以创建用户。

> db.createUser({ user: "root", pwd: "rootpw", roles: [ { role: "root", db: "admin" } ] })
> db.createUser({user: "useradmin", pwd: "adminpw", roles: [ { role: "userAdmin", db: "admin" } ] })

like @Aaron Castro's answer. 就像@Aaron Castro的回答一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM