I have deployed a web application that uses MongoDB as its database. It was successfully deployed with no issue for the past 4 months till recently it started having issue connecting to MongoDB out of the sudden on 23rd Feb 2023… Initially, doing an app restart on the day itself was able to resolve the issue. However, the issue came back hours later and persisted and restarting the app could no longer solve the issue. Did flyio changed something that causes it to no longer able to connect to the DB even after whitelisting and allowing all ip addresses to connect at 0.0.0.0 at mongoDB side?
Hi !
I run a self hosted MongoDB and everything works fine. Even with remote instances on MongoDB Atlas.
Can you post your error log ?
Hi @AsymetricalData, thanks for your reply!
I’m using MongoDB Atlas as well, but without changing any whitelist settings the logs suddenly showed “Could not connect to any servers in your MongoDB Atlas cluster.”, which is strange.
It is a nodeJS project hosted within docker.
Here is the error logs as requested, would appreciate any advice you could provide!
[info] /usr/src/app/node_modules/mongoose/lib/connection.js:824
[info] const serverSelectionError = new ServerSelectionError();
[info] ^
[info] 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/
[info] at NativeConnection.Connection.openUri (/usr/src/app/node_modules/mongoose/lib/connection.js:824:32)
[info] at /usr/src/app/node_modules/mongoose/lib/index.js:381:10
[info] at /usr/src/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
[info] at new Promise (<anonymous>)
[info] at promiseOrCallback (/usr/src/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
[info] at Mongoose._promiseOrCallback (/usr/src/app/node_modules/mongoose/lib/index.js:1234:10)
[info] at Mongoose.connect (/usr/src/app/node_modules/mongoose/lib/index.js:380:20)
[info] at Object.<anonymous> (/usr/src/app/webapp.js:28:10)
[info] at Module._compile (node:internal/modules/cjs/loader:1191:14)
[info] at Object.Module._extensions..js (node:internal/modules/cjs/loader:1245:10) {
[info] reason: TopologyDescription {
[info] type: 'ReplicaSetNoPrimary',
[info] servers: Map(3) {
[info] '<REDACTED>.mongodb.net:27017' => ServerDescription {
[info] address: '<REDACTED>.mongodb.net:27017',
[info] type: 'Unknown',
[info] hosts: [],
[info] passives: [],
[info] arbiters: [],
[info] tags: {},
[info] minWireVersion: 0,
[info] maxWireVersion: 0,
[info] roundTripTime: -1,
[info] lastUpdateTime: 2379,
[info] lastWriteDate: 0,
[info] error: null,
[info] topologyVersion: null,
[info] setName: null,
[info] setVersion: null,
[info] electionId: null,
[info] logicalSessionTimeoutMinutes: null,
[info] primary: null,
[info] me: null,
[info] '$clusterTime': null
[info] },
[info] '<REDACTED>.mongodb.net:27017' => ServerDescription {
[info] address: '<REDACTED>.mongodb.net:27017',
[info] type: 'Unknown',
[info] hosts: [],
[info] passives: [],
[info] arbiters: [],
[info] tags: {},
[info] minWireVersion: 0,
[info] maxWireVersion: 0,
[info] roundTripTime: -1,
[info] lastUpdateTime: 2379,
[info] lastWriteDate: 0,
[info] error: null,
[info] topologyVersion: null,
[info] setName: null,
[info] setVersion: null,
[info] electionId: null,
[info] logicalSessionTimeoutMinutes: null,
[info] primary: null,
[info] me: null,
[info] '$clusterTime': null
[info] },
[info] '<REDACTED>.mongodb.net:27017' => ServerDescription {
[info] address: '<REDACTED>.mongodb.net:27017',
[info] type: 'Unknown',
[info] hosts: [],
[info] passives: [],
[info] arbiters: [],
[info] tags: {},
[info] minWireVersion: 0,
[info] maxWireVersion: 0,
[info] roundTripTime: -1,
[info] lastUpdateTime: 2379,
[info] lastWriteDate: 0,
[info] error: null,
[info] topologyVersion: null,
[info] setName: null,
[info] setVersion: null,
[info] electionId: null,
[info] logicalSessionTimeoutMinutes: null,
[info] primary: null,
[info] me: null,
[info] '$clusterTime': null
[info] }
[info] },
[info] stale: false,
[info] compatible: true,
[info] heartbeatFrequencyMS: 10000,
[info] localThresholdMS: 15,
[info] setName: 'atlas-<REDACTED>-shard-0',
[info] maxElectionId: null,
[info] maxSetVersion: null,
[info] commonWireVersion: 0,
[info] logicalSessionTimeoutMinutes: null
[info] },
[info] code: undefined
[info] }
[info] Starting clean up.
[info] Umounting /dev/vdc from /usr/src/app/uploads
Do you have a dedicated IPv4 on your VM ?
My most recents projects don’t have one, maybe it’s the issue !
That make sense… However I tried to remove my dedicated IPv4 with flyctl ips release
but still no luck with mongoDB connection…
But maybe I can try to explore other options related to the IPv6
The fly ips
commands are for inbound IPs, they don’t apply to outbound requests. You can’t use the MongoDB Atlas IP whitelisting feature with your Fly.io hosted app, unfortunately, because there’s no way to ensure you’re connecting from static IPs.
Apparently it was an issue with the machine. Not sure what was the issue as everything works fine now after deleting and recreating the machine. And yes, you’re right on the inbound IPs. I forgot to mentioned that we uses http.get() to get the actual outbound ip to be used for whitelisting in mongoDB. Anyway, thanks for sharing.
But does the outbound IP addresses change?
tried this method, but seems to struggle as fly.io will use the closes server, when you use MongoDB and multiple fly.io servers seems it jumps form one to another like LAX, SYD randomly.
I have done my research and found that these ip addresses that Fly.io are using:
172.19.129.114 - EU server according to Internet Assigned Numbers Authority (IANA)
NetRange: 172.16.0.0 - 172.31.255.255
CIDR: 172.16.0.0/12
NetName: PRIVATE-ADDRESS-BBLK-RFC1918-IANA-RESERVED
NetHandle: NET-172-16-0-0-1
Parent: NET172 (NET-172-0-0-0-0)
NetType: IANA Special Use
OriginAS:
Organization: Internet Assigned Numbers Authority (IANA)
RegDate: 1994-03-15
Updated: 2013-08-30
Comment: These addresses are in use by many millions of independently operated networks, which might be as small as a single computer connected to a home gateway, and are automatically configured in hundreds of millions of devices. They are only intended for use within a private context and traffic that needs to cross the Internet will need to use a different, unique address.
Now I don’t know if this is correct, but interesting what a whois search shows. Would be great if someone from Fly.io would explain how their network is works.