Mongo-Connector无法连接MongoDB副本集至Elasticsearch求助
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:
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-3Note down each IP (e.g.,
172.18.0.2,172.18.0.3,172.18.0.4).Edit your /etc/hosts file
Open the file with sudo privileges:sudo nano /etc/hostsAdd 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-3Save and exit (press
Ctrl+O, hit Enter, thenCtrl+X).Re-run your mongo-connector command
The container names will now resolve correctly, and the connection should work.
Solution 2: Run mongo-connector in Docker (Recommended, More Stable)
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:
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"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

