MongoDB replica set with authentication fails to start
See original GitHub issueHow do we setup authentication or define a root user?
So far I have tried:
- Setting
MONGO_INITDB_ROOT_USERNAME
andMONGO_INITDB_ROOT_PASSWORD
environment variables on mongo container. Mongo container fails to start with “Error: couldn’t add user: not master :” which is fair since we pass--replSet
flag and it tries adding a user before rs.initiate(); - Passing
--auth
flag to mongod and running post start script on container to add a root user:
sleep 5;
mongo --eval 'rs.initiate()';
mongo --eval 'db=db.getSiblingDB("admin");db.createUser({ user: "root", pwd: "root", roles: [{ role: "root", db: "admin" }]})';
The user is added successfully but sidecar fails with:
The hosts mongo-0:27017 and 10.0.12.57:27017 all map to this node in new configuration version 2 for replica set rs0
as it tries to add the same replica again as mongo-0
is 10.0.12.57
.
So what’s the proper way of enabling authentication?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:9 (1 by maintainers)
Top Results From Across the Web
MongoDB authentication failed for Replica Set - Drivers & ODMs
This is really concerning as single node login always works for us but login as Replica Set with read preference as slave fails...
Read more >MongoDB not able to authenticate on the replicaset
I can't initiate the replica set unless I get access to the db. No, first you need to initiate the Replica Set, then...
Read more >Can't auth admin db on the replica set
After first user is created to RS, localhost exception is gone and you need authenticate. So, first created user MUST be admin with...
Read more >Authentication issue on MongoDB 4.2 replicaset #3782 - GitHub
The cause of the problem is, that MongoDB Community Operator is constrained to SCRAM-SHA-256 whereas Orion only uses SCRAM-SHA-1... There is a workaround...
Read more >db.auth() keeps failing for STARTUP2 - Google Groups
Create a single replica set node with auth, with user “x” as admin · Create a standalone node with auth, with user “y”...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ve come up with some kind of a workaround:
The idea is to start a container without
--replSet
(to be able to add a user withoutrs.initiate()
), create a key file on container start and add a user on primary node (mongo-0
) and restart container with--keyFile
and--replSet
.Please let me know if there is a proper way of doing this.
@nrobert13 Thanks. I have enabled authentication with similar way.