java 获取MongoCommandException:命令失败,出现错误18(AuthenticationFailed):'验证失败,'在服务器localhost:27017上

toiithl6  于 2023-04-28  发布在  Java
关注(0)|答案(4)|浏览(310)

我想与运行在docker中的MongoDB数据库建立连接。应用程序启动似乎没有任何问题,但当我试图调用任何请求,如简单的GET:

localhost:8082/devices

我收到两个不同的错误,具体取决于配置:
具有以下特性:

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

我收到:

com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'command find requires authentication' on server localhost:27017

例如 www.example.com 不同配置:

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" }

我创建了我的MongoDB Docker容器,如下所示:

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>

在docker hub的文档中,我发现了类似这样的内容:

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.

所以如果我得到正确的,我的基本用户mongoadmin的角色没有问题。
我试图合并多个配置选项,但它没有产生理想的效果。我将非常感谢关于如何解决MongoDB配置和连接建立问题的建议。

bweufnob

bweufnob1#

我在@Valijon的支持下找到了一个解决方案。我们不能使用ROOT_USERNAME来读取数据库。我们的root用户可以用来创建新的数据库、集合和新用户。解决办法真的很简单。我们应该用下面的命令打开我们的docker

docker exec -it mongo bash

和flag -it,这意味着它将是一个交互式终端,毕竟,我们将能够与我们的容器对话。毕竟,我们使用类似的mongo命令来建立根管理员凭据的身份验证。

mongo -u mongoadmin

在下一个命令行中,我们传递密码。
毕竟,我们使用的是命令:

use interviewTest

选择数据库中,我们将添加一个自定义用户。
现在,我们可以输入已经准备好的脚本,其中包含自定义密码,用户名和角色:

db.createUser({user: "testUser", pwd: "pwd", roles : [{role: "readWrite", db: "interviewTest"}]});

现在一切正常,我们可以使用我们的数据库没有身份验证错误。

lawou6xi

lawou6xi2#

如果你使用admin作为用户,那么你不需要给予密码,下面的属性对我来说很好:

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.username=admin
spring.data.mongodb.database=mydb
jvidinwx

jvidinwx3#

这份工作对我来说:

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.username=root
spring.data.mongodb.password=secret
yzckvree

yzckvree4#

这对我很有效:

mongodb://<user>:<password>@<host>:<port>/<dbname>?authSource=admin

相关问题