Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cPanel Node.js app cannot connect to MongoDB Atlas cluster but works on Heroku

I created a website with Node.js that is using a MongoDB Atlas cluster as its database. So far I hosted and tested everything on Heroku where I didn't have any problems whatsoever.

2 days ago I finally decided to buy the domain that I want and pay for proper hosting. I checked out my Git repository on my host server and used the cPanel -> Software -> Setup Node.js App tool to get my website online.

enter image description here

I used my checked-out repository as the app root, configured all environment variables (not on the picture) and executed the NPM installations.

For some reason however I cannot establish a connection to my MongoDB database, even though I whitelisted every IP address (0.0.0.0/0).

Here is the code in my app.js

const connectionString = 'mongodb+srv://' + process.env.MONGODB_USER + ':' + process.env.MONGODB_PW + '@<REDACTED_APP>-y1llv.mongodb.net/<REDACTED_APP>';
mongoose
    .connect(connectionString, {
    useUnifiedTopology: true,
    useNewUrlParser: true,
    useFindAndModify: false
    })
    .then(() => console.log('Database connected.'))
    .catch(err => console.log(err));

When I start my app and open the URL, i get this error:

App 793847 output: Server has started successfully.
App 793847 output: 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/
App 793847 output:     at NativeConnection.Connection.openUri (/home/<REDACTED_USER>/nodevenv/repositories/<REDACTED_APP>/12/lib/node_modules/mongoose/lib/connection.js:830:32)
App 793847 output:     at Mongoose.connect (/home/<REDACTED_USER>/nodevenv/repositories/<REDACTED_APP>/12/lib/node_modules/mongoose/lib/index.js:335:15)
App 793847 output:     at Object.<anonymous> (/home/<REDACTED_USER>/repositories/<REDACTED_APP>/app.js:29:6)
App 793847 output:     at Module._compile (internal/modules/cjs/loader.js:936:30)
App 793847 output:     at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
App 793847 output:     at Module.load (internal/modules/cjs/loader.js:790:32)
App 793847 output:     at Function.Module._load (internal/modules/cjs/loader.js:703:12)
App 793847 output:     at Module.require (internal/modules/cjs/loader.js:830:19)
App 793847 output:     at Module.require (/opt/passenger-5.3.7-5.el6.cloudlinux/src/helper-scripts/node-loader.js:80:25)
App 793847 output:     at require (internal/modules/cjs/helpers.js:68:18)
App 793847 output:     at loadApplication (/opt/passenger-5.3.7-5.el6.cloudlinux/src/helper-scripts/node-loader.js:243:2)
App 793847 output:     at setupEnvironment (/opt/passenger-5.3.7-5.el6.cloudlinux/src/helper-scripts/node-loader.js:214:2)
App 793847 output:     at Object.<anonymous> (/opt/passenger-5.3.7-5.el6.cloudlinux/src/helper-scripts/node-loader.js:133:1)
App 793847 output:     at Module._compile (internal/modules/cjs/loader.js:936:30)
App 793847 output:     at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
App 793847 output:     at Module.load (internal/modules/cjs/loader.js:790:32) {
App 793847 output:   message: "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/",
App 793847 output:   reason: TopologyDescription {
App 793847 output:     type: 'ReplicaSetNoPrimary',
App 793847 output:     setName: null,
App 793847 output:     maxSetVersion: null,
App 793847 output:     maxElectionId: null,
App 793847 output:     servers: Map {
App 793847 output:       '<REDACTED_APP>-shard-00-00-y1llv.mongodb.net:27017' => [ServerDescription],
App 793847 output:       '<REDACTED_APP>-shard-00-02-y1llv.mongodb.net:27017' => [ServerDescription],
App 793847 output:       '<REDACTED_APP>-shard-00-01-y1llv.mongodb.net:27017' => [ServerDescription]
App 793847 output:     },
App 793847 output:     stale: false,
App 793847 output:     compatible: true,
App 793847 output:     compatibilityError: null,
App 793847 output:     logicalSessionTimeoutMinutes: null,
App 793847 output:     heartbeatFrequencyMS: 10000,
App 793847 output:     localThresholdMS: 15,
App 793847 output:     commonWireVersion: null
App 793847 output:   }
App 793847 output: }

After a bit of searching I found some people having issues using the useUnifiedTopology flag in their connection options. When I comment it out I get this error instead:

App 811175 output: Server has started successfully.
App 811175 output: (node:811175) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
App 811175 output: MongoNetworkError: failed to connect to server [<REDACTED_APP>-shard-00-00-y1llv.mongodb.net:27017] on first connect [Error: connect ECONNREFUSED <REDACTED_IP>:27017
App 811175 output:     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1056:14) {
App 811175 output:   name: 'MongoNetworkError'
App 811175 output: }]
App 811175 output:     at Pool.<anonymous> (/home/<REDACTED_USER>/nodevenv/repositories/<REDACTED_APP>/12/lib/node_modules/mongodb/lib/core/topologies/server.js:438:11)
App 811175 output:     at Pool.emit (events.js:209:13)
App 811175 output:     at /home/<REDACTED_USER>/nodevenv/repositories/<REDACTED_APP>/12/lib/node_modules/mongodb/lib/core/connection/pool.js:562:14
App 811175 output:     at /home/<REDACTED_USER>/nodevenv/repositories/<REDACTED_APP>/12/lib/node_modules/mongodb/lib/core/connection/pool.js:1009:9
App 811175 output:     at /home/<REDACTED_USER>/nodevenv/repositories/<REDACTED_APP>/12/lib/node_modules/mongodb/lib/core/connection/connect.js:31:7
App 811175 output:     at callback (/home/<REDACTED_USER>/nodevenv/repositories/<REDACTED_APP>/12/lib/node_modules/mongodb/lib/core/connection/connect.js:264:5)
App 811175 output:     at TLSSocket.<anonymous> (/home/<REDACTED_USER>/nodevenv/repositories/<REDACTED_APP>/12/lib/node_modules/mongodb/lib/core/connection/connect.js:294:7)
App 811175 output:     at Object.onceWrapper (events.js:297:20)
App 811175 output:     at TLSSocket.emit (events.js:209:13)
App 811175 output:     at emitErrorNT (internal/streams/destroy.js:91:8)
App 811175 output:     at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
App 811175 output:     at processTicksAndRejections (internal/process/task_queues.js:77:11) {
App 811175 output:   name: 'MongoNetworkError'
App 811175 output: }

I do not believe it is an issue but the latest Node.js version I could configure in the cPanel tool was 12.9.0 while my app uses 12.16.2. I still attempted to change the engine field in my package.json file without results.

I also noticed the 'Server has started successfully.' log only appears in my passenger.log file after I open the URL, not immediately when I start the app (it is logged when I call 'app.listen(port)' in my app.js).

I have no idea where or what to look for anymore, especially since I don't have any issues running my app on Heroku. That makes me think that the issue could probably lie somewhere within the cPanel settings. Since the code and everything is the same, this is the most apparent difference to Heroku, where no cPanel exists. Maybe there's some security setting that is preventing me from establishing the connection inside cPanel.

I really hope someone here knows how to solve this :)

like image 600
blackcatalive Avatar asked Jan 25 '23 22:01

blackcatalive


2 Answers

I also contacted them too. They had to open port 27017 3000 80 443

like image 142
Onyejiaku Theodore Kelechukwu Avatar answered Jan 30 '23 08:01

Onyejiaku Theodore Kelechukwu


I contacted my host's support and they opened the 27017 port for me. Now it's all working fine! I gotta make a shoutout, Namecheap has awesome live chat support employees. They looked at my log file and everything and have been very patient.

like image 20
blackcatalive Avatar answered Jan 30 '23 10:01

blackcatalive