when I try to connect spring boot application with the mongo-db container then the authentication failed exception has thrown.
2021-05-30 02:43:51.909 INFO 1 --- [nio-9090-exec-3] org.mongodb.driver.connection : Closed connection [connectionId{localValue:5}] to mongo-db:27017 because there was a socket exception raised by this connection.
2021-05-30 02:43:51.924 ERROR 1 --- [nio-9090-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='benz', source='admin', password=<hidden>, mechanismProperties=<hidden>}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='benz', source='admin', password=<hidden>, mechanismProperties=<hidden>}] with root cause
com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server mongo-db:27017. The full response is {"ok": 0.0, "errmsg": "Authentication failed.", "code": 18, "codeName": "AuthenticationFailed"}
at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:175) ~[mongodb-driver-core-4.2.3.jar!/:na]
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:358) ~[mongodb-driver-core-4.2.3.jar!/:na]
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:279) ~[mongodb-driver-core-4.2.3.jar!/:na]
We hope that the problem has been resolved. So, we can close this thread for now.!!
In case of any further issues or queries, please feel free to reach out by creating new post in a relevant category.
Be sure, the thread will not be closed before getting resolved.
I have moved the thread to a specific category and added some tags for broader visibility.
@Nafaz_M_N_M did you resolve the issue?
I get the same error as you did. I can connect by shell using the same connection details, but not via Spring application.
My application.properties specify:
spring.data.mongodb.authentication-database=admin
UPDATE:
I have now resolved my problem. This is what happend:
I wanted to learn how to use a MongoDB instance from a Docker container instead of using my local MongoDB. I mapped the MongoDB container’s port 27017 to the computers localhost 27017. When running the Spring Application, MongoDB seemed to respond. It was only when I tried to performe CRUD-operations that authentication failed. Finally I realized that the localhost 27017 was already in use by the local MongoDB. Thus, I mapped the MongoDB container to port 8094 on localhost, and now it seems to work fine. I hope this could be of any help to anyone.
I seem to be having the same problem, I just fired a spring jpa mogo app and I have authentication issues when I try to perform any crud operation, I am running mongo on docker and I dont have any instances running locally also I checked my ports and one other app is using it. I am able to log on successfully when I using the mongo app.
By the way running spring 2.7.4 with java 17 on an m1 mac
The error I get is shown below
2022-12-24 13:30:35.002 ERROR 86462 — [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path threw exception [Request processing failed; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName=‘adebola’, source=‘onecard’, password=, mechanismProperties=}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName=‘adebola’, source=‘onecard’, password=, mechanismProperties=}] with root cause
com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): ‘Authentication failed.’ on server localhost:27017. The full response is {“ok”: 0.0, “errmsg”: “Authentication failed.”, “code”: 18, “codeName”: “AuthenticationFailed”}
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” .
Don’t use the database name at the end of the URI, mention username password and port, like this
if you’re using outside Mongodb container spring.data.mongodb.uri=mongodb://admin:adminpassword@localhost:27017/ spring.data.mongodb.database=database_name
if you’re using inside MongoDB container spring.data.mongodb.uri=mongodb://admin:adminpassword@mongodb:27017/