Adding user authentication on MongoDB replica set
a] Go to mongo shell-
Stop the secondary first using below commands:
use admin
db.shutdownServer()
b] Go to Linux shell-
sudo service mongod stop
C] Go to primary server dbe1 first and then secondary , follow below steps:
1) Config file path : /etc/mongod.conf
a) security :
authorize : enabled
b) bindIp: 127.0.0.1 (backend server's IP)
c) Restart your MongoDB service
sudo service mongod restart
2) Go to mongo shell and follow below steps :
use database1
db.createUser(
{
user: "user1",
pwd: "******",
roles: [ { role: "root", db: "database1" } ]
}
)
3) Exit from the command prompt and try connecting using below command on the mongo shell:
mongo --port 27017 -u "user1" -p "admin123" --authenticationDatabase "database1"
* Go to primary shell and follow below steps :
rs.initiate()
rs.add("dbe2:27023")
rs.add("dbe3:27024")
rs.add({ "_id" : 3, "host" : "dbw1:27025", "priority" : 0, "hidden" : true })
rs.status()
* Note : Assuming dbe1 as primary, dbe2 as secondary, dbe3 as secondary, dbw1 as hidden