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

Mongo-Connector无法连接MongoDB副本集至Elasticsearch求助

Fixing ServerSelectionTimeoutError with mongo-connector and Dockerized MongoDB Replica Set

Let's break down why you're hitting this error and how to fix it:

The Root Cause

When you connect to a MongoDB replica set, the primary node returns the full list of replica set members (from your replica.js config: mongo-rs0-1:27017, mongo-rs0-2:27017, mongo-rs0-3:27017) to the client (mongo-connector). Your OS X host doesn't know how to resolve these container names to IP addresses, so it throws the nodename nor servname provided error.

Solution 1: Update Your Hosts File (For Running mongo-connector on Host)

This lets your local machine map the container names to their internal Docker IPs:

  1. Get the IP addresses of your MongoDB containers
    Run these commands in your terminal to fetch each container's IP:

    docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mongo-rs0-1
    docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mongo-rs0-2
    docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mongo-rs0-3
    

    Note down each IP (e.g., 172.18.0.2, 172.18.0.3, 172.18.0.4).

  2. Edit your /etc/hosts file
    Open the file with sudo privileges:

    sudo nano /etc/hosts
    

    Add these lines at the bottom (replace the IPs with your actual container IPs):

    # MongoDB Replica Set Container Mapping
    172.18.0.2 mongo-rs0-1
    172.18.0.3 mongo-rs0-2
    172.18.0.4 mongo-rs0-3
    

    Save and exit (press Ctrl+O, hit Enter, then Ctrl+X).

  3. Re-run your mongo-connector command
    The container names will now resolve correctly, and the connection should work.

Since Docker Compose creates an internal network where containers can resolve each other by name, adding mongo-connector as a service to your docker-compose.yml avoids needing to edit hosts files entirely:

  1. Update your docker-compose.yml
    Add this new service block (adjust the Elasticsearch target based on where it's running):

    version: '3'
    services:
      # Keep all your existing services (mongo-rs0-1, mongo-rs0-2, etc.)
      mongo-connector:
        image: mongo-connector:latest
        # If Elasticsearch is running on your host, use host.docker.internal (OS X Docker's alias for the host)
        # If Elasticsearch is another Docker container, replace with its service name
        command: mongo-connector -m mongo-rs0-1:27017 -t host.docker.internal:9200 -d elastic2_doc_manager
        depends_on:
          - mongo-rs0-1
          - setup-rs
        extra_hosts:
          - "host.docker.internal:host-gateway"
    
  2. Start the new service
    Run this command to spin up the mongo-connector container:

    docker-compose up -d mongo-connector
    

Quick Compatibility Check

Make sure your mongo-connector version supports MongoDB 3.6.4. Older versions might have compatibility issues with older MongoDB releases, so double-check the version if you run into additional errors.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 06:49:22