我想与在 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 配置和连接建立问题的建议,我将不胜感激。
原文由 Martin 发布,翻译遵循 CC BY-SA 4.0 许可协议
我找到了@Valijon 支持的解决方案。原来我们不能使用 ROOT_USERNAME 来读出数据库。我们的根用户可用于创建新数据库、集合和新用户。解决方案非常简单。我们应该使用以下命令打开我们的 docker
和标志 -it 这意味着它将是一个交互式终端,毕竟,我们将能够与我们的容器交谈。毕竟,我们正在使用类似的 mongo 命令来使用 root 管理员凭据建立身份验证。
在下一个命令行中,我们传递密码。
毕竟,我们正在使用命令:
选择我们将在其中添加自定义用户的数据库。
现在我们可以输入已经准备好的脚本,其中包含自定义密码、用户名和角色:
现在一切正常,我们可以使用我们的数据库而不会出现身份验证错误。