0

Just working locally on a Windows machine, with a Java Spring Boot 3.1.0 app and a latest mongo db version, and getting failure when connecting the app with database.

The db is deployed locally with the next docker command:

docker run -d -p 27017:27017 --name mongo-db -e MONGO_INITDB_ROOT_USERNAME=adrian -e MONGO_INITDB_ROOT_PASSWORD=password -e MONGO_INITDB_DATABASE=documents mongo:latest

And the app has next connection properties to the database:

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=documents
spring.data.mongodb.username=adrian
spring.data.mongodb.password=password
spring.data.mongodb.authentication-database=admin

Or using uri:

spring.data.mongodb.uri=mongodb://adrian:password@localhost:27017/documents?authSource=admin

When any operation is performed through the database, this next error is printed:

org.springframework.data.mongodb.UncategorizedMongoDbException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='adrian', source='documents', password=<hidden>, mechanismProperties=<hidden>}

Also tried to create database manually after running the container, and then initializing the app, but the problem persists:

docker exec -it dd1b012c0e1a /bin/bash
use admin
db.auth('adrian', 'password')
use documents

Anyone knows, which is the source of the problem?

arevilla009
  • 429
  • 3
  • 18
  • I have no clue about Docker, but I assume the user is created in database `documents` rather than `admin`. See https://stackoverflow.com/questions/63754742/authentication-failure-while-trying-to-save-to-mongodb/63755470#63755470 – Wernfried Domscheit Aug 09 '23 at 19:44

1 Answers1

0

What could be happening here is that you could be facing a configuration mix up.

  1. Either use all the properties separately and let Spring set things up for you.
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=documents
spring.data.mongodb.username=root
spring.data.mongodb.password=password
spring.data.mongodb.authentication-database=admin

or

  1. Just use the uri.
spring.data.mongodb.uri=mongodb://root:password@localhost:27017/documents?authSource=admin

Also for good measure, you might want to also intialize your DB when you run the Docker instance.

-e MONGO_INITDB_DATABASE=documents
Hermann Steidel
  • 1,000
  • 10
  • 18
  • Hey @Hermann Steidel, thanks for your reply. I just tried initializing db when running docker instance and with both cases, with uri and properties separately, but the error persists. Any idea what could be? – arevilla009 Aug 09 '23 at 15:44
  • Just a shot in the dark but, try changing the username from `root` to something else like `admin`. You'll have to do it on the container as well of course! – Hermann Steidel Aug 09 '23 at 16:02
  • Tried but the problem persists. I tried to initialize database manually once the container is running (code in question edited), but nothing :( – arevilla009 Aug 09 '23 at 16:31
  • Looks like there might a bug in 3.1.0? https://github.com/spring-projects/spring-boot/issues/35632 Try moving to Spring 3.1.1. Worth a shot. – Hermann Steidel Aug 09 '23 at 16:42
  • The only other thing I can think of is in the line of this post: https://stackoverflow.com/questions/39086471/authentication-error-when-accessing-mongodb-through-spring-boot-app Where someone commented they needed to add `?authSource=admin&authMechanism=SCRAM-SHA-1` to the URI. However, based on the Mongo Documentation `SCRAM-SHA-256` is the default value. So you might want to try both. https://www.mongodb.com/docs/drivers/go/current/fundamentals/auth/#:~:text=SCRAM%2DSHA%2D256%20is%20the,algorithm%2C%20to%20authenticate%20your%20user. – Hermann Steidel Aug 09 '23 at 16:45
  • Hey I finally found the problem, it was that the port 27107 was already used by another service, that happened when the machine turned on. So when I was trying to connect, it was trying to connect to another mongo instance with other credentials not same of properties file. Here is the issue: https://www.mongodb.com/community/forums/t/mongodb-authentication-failed-with-spring-data-uri/109256/12 – arevilla009 Aug 10 '23 at 16:10
  • Interest, if that was the case, I would have suspected the `docker run` command to fail because the port was already in use. Glad you sorted it out though! – Hermann Steidel Aug 10 '23 at 16:58