4

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 :)

blackcatalive
  • 123
  • 2
  • 10
  • Hey could you make it work? I can't whitelist my server in Mongodb Atlas – Zak Jun 28 '20 at 11:16
  • @Zak sadly not yet. I'll try and contact MongoDB support on Monday. I hope they can at least point me in some direction to get more information on why I get this MongoNetworkError. Why can't you whitelist your server? – blackcatalive Jun 28 '20 at 12:26
  • I kind of forgot to include "then catch" block in mongoose.connect. Now it seems to connect but I can't access my database, nor I can access my website. Something's wrong. You can talk to your hosting's support. They helped me with the port. Don't know if that was one of the issues. But give it a try. – Zak Jun 28 '20 at 13:29
  • @Zak thanks a ton, that's what I did just today! They had to open the 27017 port on their end and now everything seems to be working for me. Sorry to hear about your issue. If you can't connect but the port is open it sounds like a whitelist issue but I suppose you would have thought of that. Do you have a log file with any specific errors that may offer some more insight? – blackcatalive Jun 29 '20 at 08:25
  • I'm glad that you could make it work. No I don't see any other error in my logs file. I don't know what or where the issue is. I've opened a post here. Could you please take a look and may be you can figure something out with my code? Here's the link : https://stackoverflow.com/questions/62623375/cant-access-cpanel-nodejs-app-which-is-connected-to-mongodb-atlas – Zak Jun 29 '20 at 08:55
  • Hey bro, did you make it work in a shared hosting package in Namecheap? – Zak Aug 26 '20 at 18:01
  • Hey, I'm having issues deploying a MERN (Node) app to Namecheap as well. I didn't use "Setup NodeJS App" option like you did, I uploaded the /build files into home/username/public_html and that renders the site but no mongo connection, and they turned the port on but still nothing. I want to try "Setup NodeJS" option like you did. However my app structure requires `npm install` done in both the /root and /root/client, as my react app is inside /root/client, but their service doesn't let you run npm install outside of the /root. Did you encounter anything like this? Any suggestions? – Cin88 Jan 15 '21 at 15:26

2 Answers2

4

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.

blackcatalive
  • 123
  • 2
  • 10
  • Hi I am using ventraIP hosting, they are not opening ports for me, they said we can whitelist IP address for either inbound or outbound. What should I do now, sorry if I feel dumb, I don't know much about networking. Thanks in advance – Tayyab Ferozi Dec 08 '20 at 21:42
  • If they can open ports for you, tell them to open the ports 27017 3000 80 443. – Onyejiaku Theodore Kelechukwu Dec 09 '20 at 18:15
4

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

  • Hey bro, did you make it work in a shared hosting package in Namecheap? – Zak Aug 26 '20 at 18:01
  • Yes It was a shared hosting package – Onyejiaku Theodore Kelechukwu Aug 28 '20 at 16:01
  • they diidnt open the ports in my shared package telling me its not possible for security reasons. weird! – Zak Aug 28 '20 at 20:44
  • Well, I guess you have to make sure you are the real owner. So that way they can help you do that. What I understand by shared hosting is like hosting two or three websites in one hosting package. So basically, as I said before, you have to confirm that you are the owner of the package. Then tell them to open the ports. If you have any questions feel free to ask. – Onyejiaku Theodore Kelechukwu Aug 29 '20 at 23:41
  • hey I did just that.But they refused. Which hosting did you use btw? I was refused by Namecheap. And thanks a lot mate for communicating and hellping me. – Zak Aug 30 '20 at 05:04
  • Yea. I am delighted that I am able to help. The plan is - Shared Hosting - Stellar – Onyejiaku Theodore Kelechukwu Aug 30 '20 at 21:04
  • I was refused for that plan. I'm also using shared hosting. I'll talk to them again. Thanks mate. – Zak Aug 31 '20 at 09:41
  • Hey, is this safe to open ports? and do I need to tell them that is it imbound or outbound or anythin like that?? – Tayyab Ferozi Dec 08 '20 at 17:35
  • 1
    @TayyabFerozi, I didn't tell them if it was imbound or outbound. But opening the ports would enable your Mongodb cluster make connection with your application. – Onyejiaku Theodore Kelechukwu Feb 10 '21 at 15:33