Wednesday, 19 April 2017

Node not connecting to hosted mongodb server, failed to connect to sever on first connect

I have a Godaddy hosted website that I'm trying to have node connect from to my Godaddy Mongo server. I can get it to connect to the hosted server if I run on my mac os localhost with the exact same files. I'm running a MEAN stack app on my hosted site, and the mongo server is using bitnami. When I SSH into my godaddy hosted site, then run node server/server.js it gives me the following error:

toldhandyman@a2plcpnl0843 [~/public_html]$ node server/server.js

events.js:163
  throw er; // Unhandled 'error' event
  ^
MongoError: failed to connect to server [45.40.179.118:27017] on 
first connect [MongoError: connect ECONNREFUSED 45.40.179.118:27017]
at Pool.<anonymous> 
(/home/toldhandyman/public_html/node_modules/mongodb-
core/lib/topologies/server.js:327:35)
at emitOne (events.js:96:13)
at Pool.emit (events.js:191:7)
at Connection.<anonymous> 
(/home/toldhandyman/public_html/node_modules/mongodb-
core/lib/connection/pool.js:274:12)
at Object.onceWrapper (events.js:293:19)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:194:7)
at Socket.<anonymous> 
(/home/toldhandyman/public_html/node_modules/mongodb-
core/lib/connection/connection.js:177:49)
at Object.onceWrapper (events.js:293:19)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
at emitErrorNT (net.js:1283:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)

Thats my main problem I'm having. I've been searching through this site and others for days with no success. My server.js works with my localhost using the server call:

In my config file.

MONGO_URI: 'mongodb://root:<my password here>@45.40.179.118:27017 
/told-handyman?authSource=admin',

In my server.js file.

mongoose.connect(mongoURI);

When I use the same code on my hosted site it gave me the previous error MongoError: failed to connect to server [45.40.179.118:27017] on first connect [MongoError: connect ECONNREFUSED 45.40.179.118:27017]. The Godaddy server is using Bitnami mongodb. I've tried looking through their docs, godaddy support and many other places. I've tried changing the port to 27018. Editing the file "mongodb.conf" in bitnami@told-handyman:/opt/bitnami/mongodb/mongodb.conf hasn't helped either when working with the lines:

#bind_ip = 127.0.0.1
#bind_ip = 0.0.0.0
port = 27017

I've also tried adding different users for the db, and logging in with those credentials, but that only worked on localhost. I've also tried stopping the server and restarting it a few times. I've made sure that it does get to the dbpath=/data/db.

Another problem I've noticed is getting mongod to work in my server when I SSH into it. Without running mongod, my localhost connects and works fine with the hosted server. When I run the line in my bitnami mongo server it shows:

bitnami@told-handyman:~$ mongod
2017-04-19T10:56:19.582-0700 I CONTROL  [initandlisten] MongoDB 
starting : pid=14692 port=27017 dbpath=/data/db 64-bit host=told-
handyman
2017-04-19T10:56:19.583-0700 I CONTROL  [initandlisten] db version 
v3.4.3
2017-04-19T10:56:19.583-0700 I CONTROL  [initandlisten] git version: 
f07437fb5a6cca07c10bafa78365456eb1d6d5e1
2017-04-19T10:56:19.583-0700 I CONTROL  [initandlisten] allocator: 
tcmalloc
2017-04-19T10:56:19.583-0700 I CONTROL  [initandlisten] modules: none
2017-04-19T10:56:19.584-0700 I CONTROL  [initandlisten] build 
environment:
2017-04-19T10:56:19.584-0700 I CONTROL  [initandlisten]     distarch: 
x86_64
2017-04-19T10:56:19.584-0700 I CONTROL  [initandlisten]     
target_arch: x86_64
2017-04-19T10:56:19.584-0700 I CONTROL  [initandlisten] options: {}
2017-04-19T10:56:19.611-0700 E NETWORK  [initandlisten] listen(): 
bind() failed Address already in use for socket: 0.0.0.0:27017
2017-04-19T10:56:19.612-0700 E NETWORK  [initandlisten]   addr 
already in use
2017-04-19T10:56:19.612-0700 E NETWORK  [initandlisten] Failed to set 
up sockets during startup.
2017-04-19T10:56:19.612-0700 E STORAGE  [initandlisten] Failed to set 
up listener: InternalError: Failed to set up sockets
2017-04-19T10:56:19.612-0700 I NETWORK  [initandlisten] shutdown: 
going to close listening sockets...
2017-04-19T10:56:19.612-0700 I NETWORK  [initandlisten] shutdown: 
going to flush diaglog...
2017-04-19T10:56:19.612-0700 I CONTROL  [initandlisten] now exiting
2017-04-19T10:56:19.612-0700 I CONTROL  [initandlisten] shutting down 
with code:48
bitnami@told-handyman:~$ 

It works when I run mongod --port 27018, and runs properly on localhost. I'm thinking that getting "mongod" to work right might be part of the problem in having my hosted site connect to my mongo server.

To sum up my problem, I have a Godaddy hosted website that can't connect to my Godaddy cloud Mongo server. I can SSh into both, and have node installed on the hosted site. Also running mongod on the server throws an error, but somehow the server still runs with my localhost without even entering that command.

Does anyone know how to fix this problem of connecting my server.js file through node to my mongo server? Am I missing anything about connecting a hosted website to a hosted server thats different from running both locally?

This is my first question here on SO. Thank you for your time and help.



via John L.

No comments:

Post a Comment