How to initiate authentication for a replica set - MongoDB

There is a set of replicas without authentication. I want to create its authentication for the first time.

I do the 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 a user, it throws an exception, couldn't add user: not master at src/mongo/shell/db.js:128

What should I do? Is it possible to initiate security in an existing replica set? Or should I remove the replica set and rebuild it after installing authentication.

+7
source share
3 answers

Since you are setting up replicaSet, I believe that you need to use the keyFile option, not auth = Yes. This will allow nodes in the replicaSet to interact with eachother after activating authentication.

Check out this document. http://docs.mongodb.org/manual/tutorial/enable-authentication

+2
source

although a set of replicas exists, it is not a master or master. you may not have a replica set yet.

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 can create a 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.

+2
source

If the replica set already exists, you need to find the primary node, add the user with the root role, and for each database add the user with the admin / writeAndRead / read role and / or add the admin user for all the databases.

 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 all replica nodes are synchronized. Set auth = yes for each mongod.conf file (this will force each client to use user / pass).

If you want to (optionally) add a key element to provide additional security steps between all replica sets, you can create this file, copy between each node and include the keyFile parameter inside each mongod.conf file, but this is only to force the collection nodes replicas know the secret between them and start talking, not for client applications.

Finally, restart the primary node, wait for new primary selections, and continue to restart all nodes within the replica set.

Some useful links to create a 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 on 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

+1
source

All Articles