mongo-express / mongo-express-docker

a dockerized mongo-express for viewing mongoDB in the browser
MIT License
203 stars 93 forks source link

Unable to connect to my MongoDB Atlas cluster #119

Open mathieu-benoit opened 10 months ago

mathieu-benoit commented 10 months ago

I'm doing:

cat <<EOF > compose.yaml
services:
    mongo-express:
        image: mongo-express
        restart: always
        environment:
            - ME_CONFIG_MONGODB_URL=mongodb+srv://<username-redacted>:<password-redacted>@<server-redacted>.mongodb.net/?retryWrites=true&w=majority
        ports:
          - "8081:8081"
EOF
docker compose up -d

But as soon as I hit the endpoint, if I look at the logs of my running container, I'm getting:

Waiting for <server-redacted>.mongodb.net:27017...
/docker-entrypoint.sh: line 15: <server-redacted>.mongodb.net: Name has no usable address
/docker-entrypoint.sh: line 15: /dev/tcp/<server-redacted>.mongodb.net/27017: Invalid argument
Sat Jan 13 21:32:40 UTC 2024 retrying to connect to <server-redacted>.mongodb.net:27017 (2/10)
/docker-entrypoint.sh: line 15: <server-redacted>.mongodb.net: Name has no usable address
/docker-entrypoint.sh: line 15: /dev/tcp/<server-redacted>.mongodb.net/27017: Invalid argument
...<redacted>...
Sat Jan 13 21:32:48 UTC 2024 retrying to connect to <server-redacted>.mongodb.net:27017 (10/10)
/docker-entrypoint.sh: line 15: <server-redacted>.mongodb.net: Name has no usable address
/docker-entrypoint.sh: line 15: /dev/tcp/<server-redacted>.mongodb.net/27017: Invalid argument

Am I missing anything?

BlackthornYugen commented 10 months ago

Could this be a dns issue? Can you test if the message is different if you specify the server by IP address? Or you can ping from the container to make sure dns is working. docker run -it --rm mongo-express ping something.mongodb.net

mathieu-benoit commented 10 months ago

Not sure how to get the server IP address.

I just tested:

mathieu-benoit commented 10 months ago

And for your information, if instead I do:

services:
  myapplication:
    image: mongodb/mongodb-community-server:latest
    environment:
      - CONN_STR=mongodb+srv://<username-redacted>:<password-redacted>@<server-redacted>.mongodb.net/?retryWrites=true&w=majority
    command: '/bin/bash -c "sleep 5; mongosh $$CONN_STR --eval \"show dbs;\""'

It's apparently working successfully:

Current Mongosh Log ID: 65a30858d6a2d3c3fe88995e
Connecting to:          mongodb+srv://<credentials>@<server-redacted>.mongodb.net/?retryWrites=true&w=majority&appName=mongosh+2.1.1
Using MongoDB:          6.0.12
Using Mongosh:          2.1.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

admin  336.00 KiB
local    3.22 GiB
BlackthornYugen commented 10 months ago

If the ping also fails that would confirm that the domain name cannot be resolved from the container. You can try to ping it from the host to get the ip address, or test if other dns servers work.

$ docker run -it --rm mongo-express:latest ping -c 1 google.ca
+ '[' ping '!=' mongo-express ']'
+ exec ping -c 1 google.ca
PING google.ca (142.251.33.163): 56 data bytes
64 bytes from 142.251.33.163: seq=0 ttl=62 time=41.486 ms

--- google.ca ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 41.486/41.486/41.486 ms
$ docker run -it --rm mongo-express:latest ping -c 1 this.domain.does.not.resolve
+ '[' ping '!=' mongo-express ']'
+ exec ping -c 1 this.domain.does.not.resolve
ping: bad address 'this.domain.does.not.resolve'

If ping also fails on your host machine, you might have to flush dns somewhere, maybe your router? maybe a local dns cache? You can test on a web tool like https://dns-lookup.jvns.ca/ or https://dns.google/ to see if it resolves. If it also doesn't resolve on one of those tools, then I would think the issue needs to be on mongodb.net's side.

BlackthornYugen commented 10 months ago

It looks like some ISPs will block SRV dns requests, you might want to try using a public dns server?

https://www.mongodb.com/docs/atlas/troubleshoot-connection/#internet-service-provider-dns-blocks-connection-string

To test this, try

nslookup -type=srv _ldap._tcp.google.com

or

dig SRV _ldap._tcp.google.com

If those don't work you might need to use a public dns resolver like 1.1.1.1

BlackthornYugen commented 10 months ago

Oh, I didn't think about svc records, our health check only supports A records I think. We have another bug open for that. You could get around this bug by just changing the entrypoint to node app

services:
    mongo-express:
        command: node app
        image: mongo-express
        restart: always
        environment:
            - ME_CONFIG_MONGODB_URL=mongodb+srv://<username-redacted>:<password-redacted>@<server-redacted>.mongodb.net/?retryWrites=true&w=majority
        ports:
          - "8081:8081"
mathieu-benoit commented 10 months ago

Interesting with command: node app, it's now starting, but as soon as I hit the endpoint of the app, I'm now getting:

Node.js v18.19.0
No custom config.js found, loading config.default.js
Welcome to mongo-express 1.0.2
------------------------

Could not connect to database using connectionString: mongodb+srv://<username-redacted>:****@<server-redacted>.mongodb.net/?retryWrites=true&w=majority"
/app/node_modules/mongodb/lib/sdam/topology.js:285
                const timeoutError = new error_1.MongoServerSelectionError(`Server selection timed out after ${serverSelectionTimeoutMS} ms`, this.description);

MongoServerSelectionError: connection <monitor> to 35.192.152.76:27017 closed
    at Timeout._onTimeout (/app/node_modules/mongodb/lib/sdam/topology.js:285:38)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      'ac-ss02grq-shard-00-00.1zesrse.mongodb.net:27017' => ServerDescription {
        address: 'ac-ss02grq-shard-00-00.1zesrse.mongodb.net:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 616523,
        lastWriteDate: 0,
        error: MongoNetworkError: connection <monitor> to 35.192.152.76:27017 closed
            at Connection.onClose (/app/node_modules/mongodb/lib/cmap/connection.js:134:19)
            at Socket.<anonymous> (/app/node_modules/mongodb/lib/cmap/connection.js:62:46)
            at Socket.emit (node:events:517:28)
            at TCP.<anonymous> (node:net:350:12) {
          [Symbol(errorLabels)]: Set(1) { 'ResetPool' }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        primary: null,
        me: null,
        '$clusterTime': null
      },
      'ac-ss02grq-shard-00-01.1zesrse.mongodb.net:27017' => ServerDescription {
        address: 'ac-ss02grq-shard-00-01.1zesrse.mongodb.net:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 616883,
        lastWriteDate: 0,
        error: MongoNetworkError: connection <monitor> to 35.232.105.221:27017 closed
            at Connection.onClose (/app/node_modules/mongodb/lib/cmap/connection.js:134:19)
            at Socket.<anonymous> (/app/node_modules/mongodb/lib/cmap/connection.js:62:46)
            at Socket.emit (node:events:517:28)
            at TCP.<anonymous> (node:net:350:12) {
          [Symbol(errorLabels)]: Set(1) { 'ResetPool' }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        primary: null,
        me: null,
        '$clusterTime': null
      },
      'ac-ss02grq-shard-00-02.1zesrse.mongodb.net:27017' => ServerDescription {
        address: 'ac-ss02grq-shard-00-02.1zesrse.mongodb.net:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 616336,
        lastWriteDate: 0,
        error: MongoNetworkError: connection <monitor> to 35.193.138.167:27017 closed
            at Connection.onClose (/app/node_modules/mongodb/lib/cmap/connection.js:134:19)
            at Socket.<anonymous> (/app/node_modules/mongodb/lib/cmap/connection.js:62:46)
            at Socket.emit (node:events:517:28)
            at TCP.<anonymous> (node:net:350:12) {
          [Symbol(errorLabels)]: Set(1) { 'ResetPool' }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        primary: null,
        me: null,
        '$clusterTime': null
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'atlas-flbg3z-shard-0',
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined,
  [Symbol(errorLabels)]: Set(0) {}
}
juggernaut451 commented 10 months ago

getting same error with atlas url

juggernaut451 commented 10 months ago

add the below to the environment ME_CONFIG_MONGODB_SSL: true