MongoDB Authentication Failed With Spring Data URI

I had the same problem with spring boot and solved it like this:
replicaset “rset”
username user
password pw
Dtatabase workdb
i have mongodb on pc “mini-pc” and 2 replica “hostReplica1” “hostReplica2” .

THIS NOT WORK

  • spring.data.mongodb.uri=mongodb://user:pw@mini-pc:27017,hostReplica1:27017,hostReplica2:27017/workdb?authSource=admin?replicaSet=rset

ERROR 18 authentication fail

THIS NOT WORK

  • spring.data.mongodb.uri=mongodb://user:pw@mini-pc:27017,hostReplica1:27017,hostReplica2:27017/workdb?replicaSet=rset?authSource=admin

IN THIS CASE IT START BUT DIE AFTER SOME TIME FOR REPLICASET ERROR

THIS WORK

  • spring.data.mongodb.uri=mongodb://user:pw@mini-pc:27017,hostReplica1:27017,hostReplica2:27017/workdb?authSource=admin

Hopefully it helps someone

1 Like