I got weird error with MongoDB and Spring Boot. I use Reactive MongoDb library (spring-boot-starter-data-mongodb-reactive) . It works well when I put url in application.yml file.
spring:
data:
mongodb:
uri: mongodb://test01:test01@localhost/test_db
But when I move it into MongoConfig class, application throws exception
@EnableReactiveMongoRepositories
public class MongoDBConfig extends AbstractReactiveMongoConfiguration {
@Bean
public MongoClient mongoClient() {
return MongoClients.create("mongodb://test01:test01@localhost/test_db?authMode=SCRAM-SHA-1");
}
@Override
protected String getDatabaseName() {
return "test_db";
}
}
The exception
Caused by: com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'command find requires authentication' on server localhost:27017
at com.mongodb.internal.operation.FindOperation$3.onResult(FindOperation.java:722) ~[mongodb-driver-core-4.0.4.jar:na]
at com.mongodb.internal.operation.OperationHelper$ReferenceCountedReleasingWrappedCallback.onResult(OperationHelper.java:411) ~[mongodb-driver-core-4.0.4.jar:na]
at com.mongodb.internal.operation.CommandOperationHelper$10.checkRetryableException(CommandOperationHelper.java:493) ~[mongodb-driver-core-4.0.4.jar:na]
It seems the connection strings not work when use in code.
Do you have any idea? Please help.