gpt4 book ai didi

java - 获取 MongoCommandException : Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server localhost:27017

转载 作者:行者123 更新时间:2023-12-02 18:44:36 26 4
gpt4 key购买 nike

我想与我在 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

以及具有不同配置的 application.properties:
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 配置和连接建立解决该问题的建议。

最佳答案

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

docker exec -it mongo bash

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

在下一个命令行中,我们传递密码。

毕竟,我们正在使用命令:
use interviewTest

选择我们将添加自定义用户的数据库。

现在我们可以输入已经准备好的带有自定义密码、用户名和角色的脚本:
db.createUser({user: "testUser", pwd: "pwd", roles : [{role: "readWrite", db: "interviewTest"}]});

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

关于java - 获取 MongoCommandException : Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server localhost:27017,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57744557/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com