30

I am getting this error socket exception raised by this connection while running my application.

INFO 5231 --- [nio-8087-exec-1] org.mongodb.driver.connection: Closed connection [connectionId{localValue:2}] to 192.168.0.2:27017 because there was a socket exception raised by this connection.**

    org.springframework.data.mongodb.UncategorizedMongoDbException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='admin', source='campbell', password=<hidden>, mechanismProperties={}}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='admin', source='campbell', password=<hidden>, mechanismProperties={}}
        at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:138)
        at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2756)
        at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2666)
        at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2409)
        at org.springframework.data.mongodb.core.ExecutableFindOperationSupport$ExecutableFindSupport.doFind(ExecutableFindOperationSupport.java:214)

        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

    Caused by: com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='admin', source='campbell', password=<hidden>, mechanismProperties={}}

    Caused by: com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server 192.168.0.2:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }

**I have declared properties in application properties-**
spring.data.mongodb.database=dbname
spring.data.mongodb.username=admin
spring.data.mongodb.password=*******
spring.data.mongodb.port=27017
spring.data.mongodb.host=192.168.0.2

Login credentials are right still I am getting this error. Exception authenticating MongoCredential also I am getting Uncategorized Mongo Db Exception

Please help me out!

Neodan
  • 5,154
  • 2
  • 27
  • 38
Sonal
  • 345
  • 1
  • 4
  • 8
  • Have you specified an `authenticationDatabase`? It's likely you're authenticating against a different database as to what the user is in. Check out https://stackoverflow.com/questions/46934427/connect-to-multiple-mongo-db-hosts-and-authenticate-using-a-different-database-i – Robert Seaman Mar 13 '19 at 08:51
  • 1
    After adding this line - **spring.data.mongodb.authentication-database = admin** in application properties. Now it is working properly. Thank You! – Sonal Mar 13 '19 at 09:49
  • That's great! I've added an answer to help future SO users. Please accept this as the correct answer if you feel this helped you. Thanks. – Robert Seaman Mar 13 '19 at 09:58

6 Answers6

54

As per #46934427, setting spring.data.mongodb.authentication-database=admin would probably fix your issue.

The default database in MongoDB is test, therefore, it's likely you are attempting to authenticate against the test database. Switching to the admin database (the common place for users to be created), might fix your issue.

Robert Seaman
  • 2,432
  • 15
  • 18
26

You could try this:

spring.data.mongodb.uri=mongodb://user:passwod@localhost/test?authSource=admin
D. Schreier
  • 1,700
  • 1
  • 22
  • 34
jefaokpta
  • 361
  • 3
  • 3
3
Add user admin into your database. 
Open installed folder and open the mongo.exe file and run below command : 
use admin
db.createUser(
{
user: "admin",
pwd: "admin",
roles: [ { role: "userAdminAnyDatabase", db: "admin" },
{ role: "dbAdminAnyDatabase", db: "admin" },
{ role: "readWriteAnyDatabase", db: "admin" } ]
}
)
Lokesh
  • 121
  • 1
  • 4
3

Put these configuration in your application properties file:

spring.data.mongodb.host= YOUR_HOST like 192.168.x.x
spring.data.mongodb.port=27017
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=admin
spring.data.mongodb.password=PASSWORD
spring.data.mongodb.database=DB_NAME
0

To complement other answers - I experienced this problem because Mongo was running on my local machine and in Docker simultaneously. These instances were both listening on the default 27017 port and I was connecting to my local Mongo instead of the one from the container.

If you are connecting to dockerized Mongo - try to temporary shut down the container and find out if any process is also listening on 27017. If indeed there is such process - make sure you don't need it and then terminate (Linux,Windows). You may also want to remove terminated processes from autostart so they won't appear again after reboot (Linux,Windows).

0

Give username and password along with the url explicitly.This will solve the issue. Check out my project from Linkedin everything you can get by exploring.

 spring:
      data:
          mongodb:
              uri: mongodb+srv://<username>:<password>@<cluster>.mongodb.net/
              username: <username>
              password: <password>
              database: <database>