Getting MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server localhost:27017
Solution 1
I found a solution with @Valijon support. Turns out that we cannot use ROOT_USERNAME to read out database. Our root user can be used to create new databases, collection, and new users. The solution is really easy. We should open our docker with the following command
docker exec -it mongo bash
and flag -it which mean that it will be an interactive terminal and after all, we will be able to talk with our container. After all, we are using mongo command like that to establish authentication with root admin credentials.
mongo -u mongoadmin
and in the next command line, we are passing password.
After all, we are using the command:
use interviewTest
to choose database in which one we will add a custom user.
Now we can type already prepared a script with a custom password, username, and roles:
db.createUser({user: "testUser", pwd: "pwd", roles : [{role: "readWrite", db: "interviewTest"}]});
Now everything works fine and we can use our database without authentication error.
Solution 2
if you are using admin
as user, then you do not need to give password, below properties works fine for me :
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.username=admin
spring.data.mongodb.database=mydb
Solution 3
this work for me:
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.username=root
spring.data.mongodb.password=secret
Comments
-
Martin almost 2 years
I want to establish a connection with my MongoDB database running in docker. Application seemingly starts without any problem but when I am trying to call any request like simple GET:
localhost:8082/devices
I am receiving two different errors depending on the configuration:
With properties:
spring.data.mongodb.authentication-database=admin spring.data.mongodb.host=interviewTest spring.data.mongodb.port=27017 spring.data.mongodb.username=mongoadmin spring.data.mongodb.password=secret server.port=8082 spring.data.mongodb.uri=mongodb://localhost:27017 spring.data.mongodb.database=interviewTest
I am receiving:
com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'command find requires authentication' on server localhost:27017
and for application.properties with different configuration:
spring.data.mongodb.authentication-database=admin spring.data.mongodb.port=27017 spring.data.mongodb.username=mongoadmin spring.data.mongodb.password=secret server.port=8082 spring.data.mongodb.database=interviewTest spring.data.mongodb.uri=mongodb://mongoadmin:secret@localhost:27017/interviewTest?retryWrites=true&w=majority
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 created my MongoDB docker container like below:
docker run -p 27017-27019:27017-27019 --name mongo -e MONGO_INITDB_ROOT_USERNAME=mongoadmin -e MONGO_INITDB_ROOT_PASSWORD=secret -e MONGO_INITDB_DATABASE=interviewTest -d mongo
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.interview</groupId> <artifactId>exercise</artifactId> <version>0.0.1-SNAPSHOT</version> <name>device</name> <description>Spring boot with MongoDB</description> <properties> <java.version>11</java.version> <hibernate.version>5.4.0.Final</hibernate.version> <mapstruct.processor.version>1.3.0.Final</mapstruct.processor.version> <mapstruct.version>1.3.0.Final</mapstruct.version> <spring.cloud.starter.netflix.hystrix.version>2.1.2.RELEASE</spring.cloud.starter.netflix.hystrix.version> <apache.common.lang.version>3.0</apache.common.lang.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>junit</groupId> <artifactId>junit</artifactId> </exclusion> <exclusion> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> </exclusion> <exclusion> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-library</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-library</artifactId> <version>2.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> <version>2.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.hibernate.ogm</groupId> <artifactId>hibernate-ogm-mongodb</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${mapstruct.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> <version>${mapstruct.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${apache.common.lang.version}</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <version>${spring.cloud.starter.netflix.hystrix.version}</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.3.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.3.2</version> <scope>test</scope> </dependency> <dependency> <groupId>de.flapdoodle.embed</groupId> <artifactId>de.flapdoodle.embed.mongo</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.11.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-surefire-provider</artifactId> <version>1.1.0</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.1.0</version> </dependency> </dependencies> <configuration> <source>11</source> <target>11</target> <annotationProcessorPaths> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${mapstruct.processor.version}</version> </path> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.6</version> </path> </annotationProcessorPaths> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <finalName>spring-boot-device-docker</finalName> </build> </project>
In the docker hub documentation I found something like:
MONGO_INITDB_ROOT_USERNAME, MONGO_INITDB_ROOT_PASSWORD These variables, used in conjunction, create a new user and set that user's password. This user is created in the admin authentication database and given the role of root, which is a "superuser" role.
so if I got it properly there is no problem with the role of my basic user mongoadmin.
I was trying to combine multiple configuration options but it did not yield desirable effect. I will be grateful for advice on how to solve that problem with MongoDB configuration and connection establish.
-
Martin over 4 years
spring.data.mongodb.authentication-database=admin spring.data.mongodb.host=localhost spring.data.mongodb.port=27017 spring.data.mongodb.username=mongoadmin spring.data.mongodb.password=secret server.port=8082 spring.data.mongodb.database=interviewTest spring.data.mongodb.uri=mongodb://mongoadmin:secret@localhost:27017/interviewTest?retryWrites=true&w=majority
With application.properties configured like this I am still receiving error 18 -
Alireza Khajavi over 4 yearsplease do not add
spring.data.mongodb.uri
-
Martin over 4 yearsThanks for the response. I checked option without URI and that yield the same error. Finally, turned out that the problem was with a user which we have to create because ROOT_USERNAME can be used only for managing database and create DB or other users with privileges. I added a user on my own after docker container was running and now everything is fine.
-
Alex White over 3 yearsYou might need the role "dbOwner" in some cases. My spring boot app required this
-
Reshan over 2 yearsI tried it and still getting the same error? :/
-
Reshan over 2 yearsfinally found the solution using Can’t connect to mongod with the newly created user. don't use
use interviewTest
to create new user in the first place. Instead try using the commanduse admin
. It works !!!