title: Getting MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server localhost:27017
body: <p>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: </p>
  
  <pre><code>localhost:8082/devices
  </code></pre>
  
  <p>I am receiving two different errors depending on the configuration:</p>
  
  <p>With properties:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I am receiving:</p>
  
  <pre><code>com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'command find requires authentication' on server localhost:27017
  </code></pre>
  
  <p>and for application.properties with different configuration:</p>
  
  <pre><code>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&amp;w=majority
  
  </code></pre>
  
  <pre><code>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" }
  </code></pre>
  
  <p>I created my MongoDB docker container like below:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>pom.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;parent&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
      &lt;version&gt;2.1.7.RELEASE&lt;/version&gt;
      &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
    &lt;/parent&gt;
    &lt;groupId&gt;com.interview&lt;/groupId&gt;
    &lt;artifactId&gt;exercise&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;name&gt;device&lt;/name&gt;
    &lt;description&gt;Spring boot with MongoDB&lt;/description&gt;
  
    &lt;properties&gt;
      &lt;java.version&gt;11&lt;/java.version&gt;
      &lt;hibernate.version&gt;5.4.0.Final&lt;/hibernate.version&gt;
      &lt;mapstruct.processor.version&gt;1.3.0.Final&lt;/mapstruct.processor.version&gt;
      &lt;mapstruct.version&gt;1.3.0.Final&lt;/mapstruct.version&gt;
      &lt;spring.cloud.starter.netflix.hystrix.version&gt;2.1.2.RELEASE&lt;/spring.cloud.starter.netflix.hystrix.version&gt;
      &lt;apache.common.lang.version&gt;3.0&lt;/apache.common.lang.version&gt;
    &lt;/properties&gt;
  
    &lt;dependencies&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
        &lt;scope&gt;test&lt;/scope&gt;
        &lt;exclusions&gt;
          &lt;exclusion&gt;
            &lt;groupId&gt;junit&lt;/groupId&gt;
            &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;/exclusion&gt;
          &lt;exclusion&gt;
            &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
            &lt;artifactId&gt;hamcrest-core&lt;/artifactId&gt;
          &lt;/exclusion&gt;
          &lt;exclusion&gt;
            &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
            &lt;artifactId&gt;hamcrest-library&lt;/artifactId&gt;
          &lt;/exclusion&gt;
        &lt;/exclusions&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
        &lt;artifactId&gt;hamcrest-library&lt;/artifactId&gt;
        &lt;version&gt;2.1&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
        &lt;artifactId&gt;hamcrest-core&lt;/artifactId&gt;
        &lt;version&gt;2.1&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
        &lt;artifactId&gt;lombok&lt;/artifactId&gt;
        &lt;optional&gt;true&lt;/optional&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.hibernate.ogm&lt;/groupId&gt;
        &lt;artifactId&gt;hibernate-ogm-mongodb&lt;/artifactId&gt;
        &lt;version&gt;${hibernate.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
        &lt;artifactId&gt;mapstruct-processor&lt;/artifactId&gt;
        &lt;version&gt;${mapstruct.version}&lt;/version&gt;
        &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
        &lt;artifactId&gt;mapstruct&lt;/artifactId&gt;
        &lt;version&gt;${mapstruct.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
        &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
        &lt;version&gt;${apache.common.lang.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
        &lt;artifactId&gt;spring-cloud-starter-netflix-hystrix&lt;/artifactId&gt;
        &lt;version&gt;${spring.cloud.starter.netflix.hystrix.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
        &lt;artifactId&gt;junit-jupiter-params&lt;/artifactId&gt;
        &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
        &lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
        &lt;version&gt;5.3.2&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
        &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
        &lt;version&gt;5.3.2&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;de.flapdoodle.embed&lt;/groupId&gt;
        &lt;artifactId&gt;de.flapdoodle.embed.mongo&lt;/artifactId&gt;
        &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.mongodb&lt;/groupId&gt;
        &lt;artifactId&gt;mongo-java-driver&lt;/artifactId&gt;
        &lt;version&gt;3.11.0&lt;/version&gt;
      &lt;/dependency&gt;
  
    &lt;/dependencies&gt;
  
    &lt;build&gt;
      &lt;plugins&gt;
        &lt;plugin&gt;
          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
          &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
          &lt;version&gt;3.8.0&lt;/version&gt;
  
          &lt;dependencies&gt;
            &lt;dependency&gt;
              &lt;groupId&gt;org.junit.platform&lt;/groupId&gt;
              &lt;artifactId&gt;junit-platform-surefire-provider&lt;/artifactId&gt;
              &lt;version&gt;1.1.0&lt;/version&gt;
            &lt;/dependency&gt;
            &lt;dependency&gt;
              &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
              &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
              &lt;version&gt;5.1.0&lt;/version&gt;
            &lt;/dependency&gt;
          &lt;/dependencies&gt;
  
          &lt;configuration&gt;
            &lt;source&gt;11&lt;/source&gt;
            &lt;target&gt;11&lt;/target&gt;
            &lt;annotationProcessorPaths&gt;
              &lt;path&gt;
                &lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
                &lt;artifactId&gt;mapstruct-processor&lt;/artifactId&gt;
                &lt;version&gt;${mapstruct.processor.version}&lt;/version&gt;
              &lt;/path&gt;
              &lt;path&gt;
                &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
                &lt;artifactId&gt;lombok&lt;/artifactId&gt;
                &lt;version&gt;1.18.6&lt;/version&gt;
              &lt;/path&gt;
            &lt;/annotationProcessorPaths&gt;
          &lt;/configuration&gt;
        &lt;/plugin&gt;
  
        &lt;plugin&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
        &lt;/plugin&gt;
      &lt;/plugins&gt;
      &lt;finalName&gt;spring-boot-device-docker&lt;/finalName&gt;
    &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>In the docker hub documentation I found something like:</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>so if I got it properly there is no problem with the role of my basic user mongoadmin. </p>
  
  <p>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. </p>