Can't connect Node.js to Mongodb - node.js

I'm newbie about node.js and MongoDB.
I have installed MongoDB on a server A. MongoDB is running and port 27017 is opened. I am able to connect to robo3T
I have a Node.js app on another server B. Node.js is running.
When I access the website, everything is displayed correctly but when I want to login (so it needs MongoDB access), the website turns and get this error: Cannot GET /502.shtml
Here are all details
Mongodb server
mongod.conf :
port: 27017
bindIp: 0.0.0.0
Nodejs app.js:
var promise = mongoose.connect('mongodb://XXXX:27017/preprod', {useMongoClient: true}, function(err) {
if (err) return console.error(err);});
mongoose.Promise = require('bluebird');
XXXX = address IP of server A (mongodb server)
when I run app.js I got this error
name: 'MongoError',
message: 'failed to connect to server [XXX:27017] on first connect [MongoError: connect ETIMEDOUT XXXX:27017]' }
I am able to connect from my linux machine, other VPS server but not to my nodejs server!
Maybe I missed something?

Do this on a Linux machine,
iptables -A INPUT -s 0.0.0.0 -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -d 0.0.0.0 -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT
and try connecting from the remote server (Server B) with the IP address of Server A.

Resolved:
Actually the port 27017 was closed on my nodejs server.
now it can connected!
if it can help someone else

mongoose.connect(mongodb://localhost:27017/<project_name>, {
useMongoClient: true
});
Where is the name of the project.
Make sure you've installed and imported mongoose. And also, that you've got mongod running.
Also, I'd remove 'var promise = ' before mongoose.connect(..)

Related

Unable to connect to Postgres on EC2: ECONNREFUSED

I have a Node server hosted on an EC2 instance that is trying to connect to Postgres running on the same instance. When I start the server I get an ECONNREFUSED error to the db:
Unable to connect to the database: SequelizeConnectionRefusedError: connect ECONNREFUSED x.x.x.x:5432
I am using the Sequelize ORM package from NPM:
import Sequelize from 'sequelize';
const db = new Sequelize('postgres://postgres#52.9.136.53/unloadx' {dialect: 'postgres'});
Postgres is running on the instance, and I have port 5432 open in my inbound security rule. I can confirm that postgres is running with ps aux | grep postgres which shows multiple postgres related processes, and netstat -anp --tcp confirms that postgres appears to be listening on the port:
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 ::1:5432 :::* LISTEN -
But when I scan the port with nmap x.x.x.x -p 5432 it shows that the port status is closed. Per nmap, this means that the port is accessible but has no application listening on it able to respond to packets.
Appreciate any help in getting the connection to work.
One problem is that you are using the public IP address. That means your database connection is basically going out to the internet and back. This will prevent the Security Group rule from working, and introduce security and latency issues.
If you were trying to connect from another server in the VPC you would want to connect using the private IP. However, since it's on the same instance you can delete the Security Group rule and just use localhost or 127.0.0.1.
What makes you think that netstat is showing that postgres is listening on the port? If it were, you'd see something in the PID/Program column.
Also, if your postgres server is running on the same box as your node app, you should not need to open up the port to the internet via your AWS security rules, and probably shouldn't unless you need to access it from elsewhere. Then you can change your connection string to hit localhost or 127.0.0.1
Finally, verify that you've setup your postgres server correctly via the docs

Error remote access MongoDB on Ubuntu server

I have an Ubuntu 14.04 Linux server with MongoDB 3.2.4 running at Digital Ocean as a droplet (one-click Apps).
Pinging the server works (droplet is distroyed after posting this):
ping 198.199.125.101
I created database test and user:
db.createUser({"user": "test", "pwd": "test", "roles": ["dbOwner"]})
In mongod.conf I changed bindIp: 0.0.0.0 and restarted mongoDB
I disabled the firewall and reboot the server. Just for test, just to prove iptables is not the issue (don't do this on a regular server):
sudo ufw disable
The problem is I can't get remote access to the mongo Database
mongo 198.199.125.101:27021/test -u "test" -p "test"
Error message (connection refused):
MongoDB shell version: 3.2.0
connecting to: 198.199.125.101:27021/test
2016-05-11T22:05:35.876+0200 W NETWORK [thread1] Failed to connect to 198.199.125.101:27021, reason: errno:61 Connection refused
2016-05-11T22:05:35.879+0200 E QUERY [thread1] Error: couldn't connect to server 198.199.125.101:27021, connection attempt failed :
connect#src/mongo/shell/mongo.js:226:14
#(connect):1:6
exception: connect failed
First run netstat on the mongo machine to verify that the port 27021 is open. netstat -anp should do it.
Then do "telnet 127.0.0.1 27021" to make sure it is open.
Once you are sure the port is open, then use telnet 198.199.125.101 27021 to verify you can connect to the mongo machine on port 27021. If you can, then it has something to do with your app. If not then something is blocking the connection. Some firewall or something. Are you on aws?
As per netstat, can you try mongo 198.199.125.101:27017/test -u "test" -p "test"

How to connect via mongoose and a ssh tunnel

I have setup my mongod.conf as follows so it only allows localhost connection.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
port: 27017
bindIp: 127.0.0.1
I then want my site to ssh into the mongodb so the port has to be converted to localhost.
However how can I integrate this with mongoose's connect function?
mongoose.connect(configDB.url, function(err){
if (err){
console.log('Error connecting to mongodb: ' + err)
}
});
I have found the following command but I am not sure if this is what I need:
ssh -L 4321:localhost:27017 -i ~/.ssh/ssh_key user#ip-adress
This should ssh me via port 4321 to the localhost right? So I think I need something like this in the nodejs mongoose's connect function. I've tried to read up on this on the mongodb security tutorials but I cannot link their instructions to nodejs at all. Anyone who has experience with this?
You're nearly there. Set up the tunnel independent of node:
ssh -Nf -p [db_server_ssh_port] [mongo_user]#[mongo_domain] -L \
[local_db_port]:localhost:[remote_db_port]
And then within node, connect to mongo using [local_db_port]:
mongoose.connect(
"mongodb://localhost:[local_db_port]/[db_name]",
{"pass":"[db_pwd]"}
)
All the traffic sent to [local_db_port] on the web server will be sent through the tunnel to port [remote_db_port] on [mongo_domain]. The following post gives more info. It's connecting to a MySQL database, but the principle is the same.
Connect to MySQL using SSH Tunneling in node-mysql
Set up the tunnel independent of node:
ssh -L [your given port]:localhost:27017 [username of ssh]#[ip address of ssh matchine] -f -N
after that you have include your given port for mongo database.
In the nodejs you have to setup for mongoose connection like this
'mongodb://localhost:[your given port number]/[database name]'
enjoy it

I cant connect to my Amazon ec2 running nodejs using tcp

Hi all I have created an Ubuntu EC2 instance and have installed nodejs on it. Am running a simple node js script (which IS running and not throwing any errors:
var net = require('net');
var server = net.createServer(function (socket) {
socket.write("from server\r\n");
socket.pipe(socket);
});
server.listen(8000, "localhost");
console.log("TCP server listening on port 8000 at localhost.");
to test this, (am on windows) I am running a program called hercules to attempt a tcp connection, but it always comes back with tcp connection error: 10061
and the IP address can't be pinged either.
My ec2's IP address is 54.76.31.140. I have added an inbound tcp:8000 0.0.0.0 rule to my security group in the aws console and I have added hercules to my windows firewall and avg exceptions. I have also added an exception to my home dsl router and finally I have checked the ubuntu iptables there are no rules set.
Pls help, I can't seem to find whats wrong here.
If you tell the server to listen on "localhost," that's the only place it will listen -- "localhost" -- the loopback interface, 127.0.0.1, which is only accessible from... the local host.
Remove the 2nd argument to server.listen().
http://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback
If you want to be able to ping your instance, you have to allow ICMP in the security group.
Also, before you fix it, and after, run this, and note the difference in output:
$ netstat -a -n | grep 8000 | grep -i tcp

Sequelize connection over ssh

I am not able to connect to a remote mysql server using sequelize but I am able to ssh into the machine and connect to mysql.
How do I make Sequelize connect to mysql server over ssh rather than directly in node?
You set up an SSH tunnel to your server, which listens on (for example) port 33060:
ssh -NL 33060:localhost:3306 yourserver
In another window/terminal, you can use the MySQL client (assuming that it's installed locally) to connect to the remote MySQL server over the SSH tunnel:
mysql --port 33060 --host 127.0.0.1
If that works, it's just a matter of changing the port number in Sequelize:
var sequelize = new Sequelize('database', 'username', 'password', {
host: "127.0.0.1",
port: 33060
});
If it doesn't work, it might be that the remote MySQL server isn't configured to accept connections over TCP. If that's the case, you should configure your MySQL server to get it to accept TCP connections.

Resources