Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb replicaset creation with authentication through docker script

I am struggling to find out the solution to authenticate my mongo db replica set through docker script.I am able to achieve the target on native mongo of server but in docker image I am not able to implement the authentication.(I am able to create the replicaset on docker image as well).

like image 842
Balwant Singh Avatar asked Oct 19 '22 12:10

Balwant Singh


1 Answers

I was facing the same issue, i had to do the process in a different order. Try setting up the authentication first and then create replications.

1.start docker mongo without replica or auth

docker run  --rm -p 22222:27017  -v datadb1:/data/db --name mongonew mongo:2.6

2.connect with mongo and add users you want. And make sure you add a superuser, we will use this user to initiate replication later

db.createUser({ user: "superuser", pwd: "superuser", roles: [ "userAdminAnyDatabase","readWriteAnyDatabase","dbAdminAnyDatabase","clusterAdmin" ]})

3.stop docker mongo and restart with replica and auth

docker run  --rm -p 22222:27017  -v datadb1:/data/db --name mongonew mongo:2.6 --replSet replocalnew --auth

4.connect with mongo now. authenticate with the superuser we created.

db.auth("superuser","superuser");

5.now initiate replication

rs.initiate();
like image 59
Ganesh Mit Avatar answered Oct 24 '22 17:10

Ganesh Mit