I have mongo instances A, B and C, each having a different set of
users with different roles. I have used mongo shell to connect with A
through an Admin user. Now, to create a replica and add B and C, do I
need to pass through auth on B and C. Can you please help me with the
commands?
As @AdamC
has been already stated that here If they are members of the same replica set, then no. The users will be written to the primary and then replicated to the secondaries - remember any secondary can become primary in a normal set, so you would have to have all the data necessary to do that, including users. If the nodes are in the set when they are added, the users will replicate normally. If you add them later, they will replicate the users as part of the initial sync process.
Suppose that you have 3 node replica set like as A,B,C
The Node A there is configuration file as (A.conf)
For example I am mentioning the node A configuration file
storage:
dbPath: /var/mongodb/db/A
net:
bindIp: 192.168.xxx.xxx,localhost
port: 27011 --- node A port no
security:
authorization: enabled
keyFile: /var/mongodb/pki/Example-keyfile
systemLog:
destination: file
path: /var/mongodb/db/A/mongod.log
logAppend: true
processManagement:
fork: true
replication:
replSetName: StackExachange-example
Node B.conf file
storage:
dbPath: /var/mongodb/db/B
net:
bindIp: 192.168.xxx.xxx,localhost
port: 27012 --- node B port no
security:
authorization: enabled
keyFile: /var/mongodb/pki/Example-keyfile
systemLog:
destination: file
path: /var/mongodb/db/B/mongod.log
logAppend: true
processManagement:
fork: true
replication:
replSetName: StackExachange-example
Node C.conf file
storage:
dbPath: /var/mongodb/db/C
net:
bindIp: 192.168.xxx.xxx,localhost
port: 27013 --- node C port no
security:
authorization: enabled
keyFile: /var/mongodb/pki/Example-keyfile
systemLog:
destination: file
path: /var/mongodb/db/C/mongod.log
logAppend: true
processManagement:
fork: true
replication:
replSetName: StackExachange-example
Note: Here the dbpath of every node (i.e., A,B,C will be different)
After making the configuration file of A,B,C with thier different path start the mongod server process.
Starting mongod processes with A.conf, B.conf and C.conf:
mongod -f A.conf
mongod -f B.conf
mongod -f C.conf
after starting the mongod process of all node, suppose that I will connect to node A
with mongo shell
mongo --port 27011 --- here port no 27011 is port no of A
Initiating the replica set:
rs.initiate()
Creating a user:
use admin
db.createUser({
user: "StackExchange-admin",
pwd: "StackExchange-pass",
roles: [
{role: "root", db: "admin"}
]
})
Note : Here I am creating the user and password of admin database of
node A.
Exiting out of the Mongo shell and connecting to the entire replica set:
exit
mongo --host "StackExchange-example/192.168.xxx.xxx:27011" -u "StackExchange-admin"
-p "StackExchange-pass" --authenticationDatabase "admin"
Getting replica set status:
rs.status()
Adding other members to replica set:
rs.add("B:27012")
rs.add("C:27013")
Getting an overview of the replica set topology:
rs.isMaster()
Stepping down the current primary:
rs.stepDown()
Checking replica set overview after election:
rs.isMaster()
I have mongo instances A, B and C, each having a different set of
users with different roles. After I create a replica with A, B and C,
what happens to the users of individual nodes. Are all the users from
each node available in the replica?
Here as I understood the mongo instances (A,B,C) means that three mongod. Suppose that among three of node A,B,C they have some different users with different roles . Suppose that when replication has been made with the node A as primary replica set with their admin database & their roles authentication.
As whatever before the replication you have the Users & roles of the the node B,C it will be remain there but after the replication if you shall create new users or roles to members of replica then need to Resync a Member of a Replica Set
For further your ref here , here and here