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?

1

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

7 Comments

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?
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!
Tried but the problem persists. I tried to initialize database manually once the container is running (code in question edited), but nothing :(
Looks like there might a bug in 3.1.0? github.com/spring-projects/spring-boot/issues/35632 Try moving to Spring 3.1.1. Worth a shot.
The only other thing I can think of is in the line of this post: stackoverflow.com/questions/39086471/… 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. mongodb.com/docs/drivers/go/current/fundamentals/auth/….
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.