Unable to connect to mongodb from GCP

Greetings. This is my first implementation of a web app, and I’m not able to successfully deploy from GCP using a Compute Engine instance.

From mongodb, I have whitelisted the instance’s external IP address. Are there other reasons I could be getting this error?

$ nodemon server.js
[nodemon] 2.0.20
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
server started on port 8080
/home/overlord/github_sleepywakes_thunderroost/node_modules/mongoose/lib/connection.js:807
  const serverSelectionError = new ServerSelectionError();
                               ^

MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
    at NativeConnection.Connection.openUri (/home/overlord/github_sleepywakes_thunderroost/node_modules/mongoose/lib/connection.js:807:32)
    at /home/overlord/github_sleepywakes_thunderroost/node_modules/mongoose/lib/index.js:340:10
    at /home/overlord/github_sleepywakes_thunderroost/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/home/overlord/github_sleepywakes_thunderroost/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (/home/overlord/github_sleepywakes_thunderroost/node_modules/mongoose/lib/index.js:1140:10)
    at Mongoose.connect (/home/overlord/github_sleepywakes_thunderroost/node_modules/mongoose/lib/index.js:339:20)
    at file:///home/overlord/github_sleepywakes_thunderroost/server.js:28:10
    at ModuleJob.run (node:internal/modules/esm/module_job:175:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      'cluster0-shard-00-00.geujl.mongodb.net:27017' => ServerDescription {
        _hostAddress: HostAddress {
          isIPv6: false,
          host: 'cluster0-shard-00-00.geujl.mongodb.net',
          port: 27017
        },
        address: 'cluster0-shard-00-00.geujl.mongodb.net:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 634944,
        lastWriteDate: 0,
        error: MongoNetworkError: connection <monitor> to 35.184.107.131:27017 closed
            at Connection.handleIssue (/home/overlord/github_sleepywakes_thunderroost/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:122:23)
            at TLSSocket.<anonymous> (/home/overlord/github_sleepywakes_thunderroost/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:63:39)
            at TLSSocket.emit (node:events:394:28)
            at node:net:662:12
            at TCP.done (node:_tls_wrap:580:7)
      },
      'cluster0-shard-00-01.geujl.mongodb.net:27017' => ServerDescription {
        _hostAddress: HostAddress {
          isIPv6: false,
          host: 'cluster0-shard-00-01.geujl.mongodb.net',
          port: 27017
        },
        address: 'cluster0-shard-00-01.geujl.mongodb.net:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 636016,
        lastWriteDate: 0,
        error: MongoNetworkError: connection <monitor> to 35.232.161.204:27017 closed
            at Connection.handleIssue (/home/overlord/github_sleepywakes_thunderroost/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:122:23)
            at TLSSocket.<anonymous> (/home/overlord/github_sleepywakes_thunderroost/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:63:39)
            at TLSSocket.emit (node:events:394:28)
            at node:net:662:12
            at TCP.done (node:_tls_wrap:580:7)
      },
      'cluster0-shard-00-02.geujl.mongodb.net:27017' => ServerDescription {
        _hostAddress: HostAddress {
          isIPv6: false,
          host: 'cluster0-shard-00-02.geujl.mongodb.net',
          port: 27017
        },
        address: 'cluster0-shard-00-02.geujl.mongodb.net:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 634879,
        lastWriteDate: 0,
        error: MongoNetworkError: connection <monitor> to 35.192.112.82:27017 closed
            at Connection.handleIssue (/home/overlord/github_sleepywakes_thunderroost/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:122:23)
            at TLSSocket.<anonymous> (/home/overlord/github_sleepywakes_thunderroost/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:63:39)
            at TLSSocket.emit (node:events:394:28)
            at node:net:662:12
            at TCP.done (node:_tls_wrap:580:7)
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'atlas-67keal-shard-0',
    logicalSessionTimeoutMinutes: undefined
  }
}
[nodemon] app crashed - waiting for file changes before starting...

This is the line from my code, using the cluster copied from mongodb.

mongoose.connect("mongodb+srv://overlord:mt5E%23c4b@cluster0.geujl.mongodb.net/ThunderDB", { useNewUrlParser: true});

Thank you in advance for any advice!

Allow access from Anywhere first and let us know the results.

Try connecting with mongosh using the same URI.

When I allow full access, I no longer crash the application. And I can connect to mongodb through Mongodb Compass. However, I still get “This site can’t be reached” in my browser when I go to the URL of the application.

Is not MongoDB related. Stackoverflow might be a better place to ask. What is the URL of the application?

Thanks, I’m certain I’m missing something simple given this is my first go at this. GCP is able to send a packet to the site, per their troubleshooting, but the URL gives me this error. Anyway, I will try Stackoverflow, thanks very much for the help here, Steeve!

This site can’t be reached

35.222.10.241 took too long to respond.

@SleepyWakes, you might want to remove access from everywhere and change your password as you’ve put your actual credentials in the connection string you posted and anyone who reads this, and wants to, can now access your system.

Granted it looks like you’re in early stages of work as there are only two collections with one document each, but still, you don’t want someone wreaking havoc on your system.

2 Likes

I cannot ping or traceroute to this IP

May be you have some firewall rules than prevents traffic. You mentioned GCP in your title. A Google Cloud Platform specific forum might exists for that.

Thanks for your patience with me, I know this must be frustrating. It turns out I gave you the wrong IP (I gave the external troubleshooting IP from GCP, ugh). It’s actually:
34.173.96.254

and appears to be pingable. I cannot figure out how I can run my server with 0.0.0.0/0 open but when I only whitelist the GCP external IP (the one above) it crashes. It’s obviously a whitelist issue, but that process is pretty simple. Could it be that my code is not properly accessing my Mongodb database?

To know which IP to open use https://www.whatismyip.com/ on the machine where you application is running.

You might be interested with Announcing Google Private Service Connect (PSC) Integration for MongoDB Atlas | MongoDB Blog.