You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows环境下Spring Boot连接MongoDB Docker容器超时问题排查

Hey there! Let's work through getting your Spring Boot app connected to that MongoDB container running in Docker on VirtualBox. I'll cover the configuration options, why your current setup might not be working, and how to fix it.

Spring Boot配置MongoDB主机与端口的方法

Absolutely, Spring Boot lets you specify MongoDB's host and port directly in configuration files—this is the standard way to set up the connection.

Using application.properties

Add these lines to your application.properties file (replace placeholders with your actual values):

# 核心连接配置
spring.data.mongodb.host=你的VirtualBox虚拟机IP地址
spring.data.mongodb.port=27017

# 如果MongoDB设置了认证,补充以下配置
spring.data.mongodb.username=your-mongo-username
spring.data.mongodb.password=your-mongo-password
spring.data.mongodb.database=your-target-database-name

Using application.yml (alternative)

If you prefer YAML over properties, here's the equivalent setup:

spring:
  data:
    mongodb:
      host: 你的VirtualBox虚拟机IP地址
      port: 27017
      username: your-mongo-username
      password: your-mongo-password
      database: your-target-database-name
为什么VM IP配置没生效?排查这些关键点

If you already added the VM IP but it's not working, the issue is likely tied to network connectivity between your Windows host, VirtualBox, and the Docker container. Let's go through these checks:

  1. Verify VirtualBox Network Mode

    • Make sure your VirtualBox VM uses Bridge Adapter mode (go to VM Settings > Network > Adapter 1). This lets the VM get an IP on your local network that's accessible from your Windows machine. If you're using NAT mode, you'll need to set up port forwarding in VirtualBox to route traffic from your Windows host to the VM's 27017 port.
  2. Check Docker Port Mapping

    • When starting the MongoDB container, did you map the container's 27017 port to the VM's 27017 port? The correct run command looks like this:
      docker run -d -p 27017:27017 --name mongodb mongo:latest
      
    • Confirm the port mapping by running docker ps in the VM's terminal. Look for the PORTS column—it should show 0.0.0.0:27017->27017/tcp (meaning the port is open to all interfaces).
  3. Test Network Connectivity

    • On your Windows host, open PowerShell and run these commands to validate connectivity:
      # 测试能否ping通虚拟机
      ping 你的VirtualBox虚拟机IP
      
      # 测试27017端口是否开放
      Test-NetConnection 你的VirtualBox虚拟机IP -Port 27017
      
    • If the ping fails or the port test shows "TcpTestSucceeded: False", you have a network block (firewall, VirtualBox settings, or Docker config) that needs fixing.
  4. Firewall Checks

    • Ensure the VM's firewall allows incoming traffic on port 27017. For most Linux VMs, you can run sudo ufw allow 27017 to open the port.
    • Also check your Windows host's firewall—make sure it doesn't block outgoing traffic to the VM's 27017 port.
Verify Your pom.xml Dependencies

Double-check that you have the correct Spring Data MongoDB starter in your pom.xml. This dependency handles all connection setup out of the box:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

You don't need additional MongoDB driver dependencies unless you're using a specific version or custom configuration—this starter includes everything you need.


内容的提问来源于stack exchange,提问作者Vaibhav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:49:21