Unable to connect to remote mongodb instance from nodejs deployed on Amazon EC2 VM - node.js

I am unable to connect to remote mongodb service deployed using Mlabs. I am able to connect to this service from the node server deployed on my local machine but it does not works when I try to deploy it on Amazon EC2 windows instance.
I have opened the following inbound and outbound rules.
enter image description here
enter image description here
I have also opened the firewall rules for that, but still it does not works.
I am trying to connect to it using mongoose in nodejs.
mongoose.connect('mongodb://user:pass#ds031947.mlab.com:31947/db');

As you said its accessible from your local system so most probably you have your bind-ip set to localhost instead of public ip.
open your /etc/mongodb.conf and make sure the bind ip is set to public access not local host.
run on EC2 instance:
netstat -pl
the output must shows
tcp 0 0 0.0.0.0:27017 : LISTEN 2025/mongod
if it shows localhost:27017 you have to change the config file to make it accessible to public.
Hope it will help !

Related

Implemented keycloak on aws ec2 windows instance and it's running inside the instance only but not running outside the instance

I'm using keycloak 15.1.1 version and 64 bit windows ec2 instance.
Downloaded RDP from aws and using RDP I logged into instance and added keycloak and mysql connecor 8.0.31 to connect keycloak with external database.
I referred from here : https://jbjerksetmyr.medium.com/how-to-setup-a-keycloak-server-with-external-mysql-database-on-aws-ecs-fargate-in-clustered-mode-9775d01cd317
I did all as here mentioned.
Note : To run keycloak they(link) used "standalone.sh" command but for windows instance :standalone" is enough.
So I ran keycloak using following command
command 1 : "standalone -b 172.31.35.208" This is my private ip
It's running successfully on 172.31.35.208:9090
Here 9090 port is mapped with 8080 so my keycloak currently running on 9090 port. But if I use this same address outside of the instance it's showing error "This site can’t be reached"
command 2 : "standalone -b http://ec2-35-180-74-78.eu-west-3.compute.amazonaws.com" this is my public DNS
running inside instance
It's running successfully on http://ec2-35-180-74-78.eu-west-3.compute.amazonaws.com:9090/
But if I use this same address outside of the instance it's showing error "This site can’t be reached"
running outside instance
Note : Here -b means, I am telling to my instance that run keycloak on my private Ip or on my public DNS
I tried this : Edited inbound rules to add port 8080 and 9090 to clear traffic but it doesn't work
Anyone can help me to solve this issue
Keycloak service is not accessible from public browser but it's only on inside the instance
Help me to find out the issue
Issue fixed. That issue came because of version 15. Then I used version 19 it's working now

Connect to host mongodb from docker container

So I want to connect to my mongodb running on my host machine (DO droplet, Ubuntu 16.04). It is running on the default 27017 port on localhost.
I then use mup to deploy my Meteor app on my DO droplet, which is using docker to run my Meteor app inside a container. So far so good.
A standard mongodb://... connection url is used to connect the app to the mongodb.
Now I have the following problem:
mongodb://...#localhost:27017... obviously does not work inside the docker container, as localhost is not the host's localhost.
I already read many stackoverflow posts on this, I already tried using:
--network="host" - did not work as it said 0.0.0.0:80 is already in use or something like that (nginx proxy)
--add-host="local:<MY-DROPLET-INTERNET-IP>" and connect via mongodb://...#local:27017...: also not working as I can access my mongodb only from localhost, not from the public IP
This has to be a common problem!
tl;dr - What is the proper way to expose the hosts localhost inside a docker container so I can connect to services running on the host? (including their ports, e.g. 27017).
I hope someone can help!
You can use: 172.17.0.1 as it is the default host ip that the containers can see. But you need to configure Mongo to listen to 0.0.0.0.
From docker 18.03 onwards the recommendation is to connect to the special DNS name host.docker.internal
For previous versions you can use DNS names docker.for.mac.localhost or docker.for.windows.localhost.
change the bindIp from 127.0.0.1 to 0.0.0.0 in /etc/mongod.conf. Then it will work
or start mongod on ubuntu with a flag to bind all ip address as a temporary workaround (dev/learning purposes)
$ mongod --bind_ip_all
Tried 100500 variants for Windows (using docker desktop), but without any result...
Unfortunately, currently, Windows (at least docker desktop) is not supporting --net=host
Quoted from: https://docs.docker.com/network/network-tutorial-host/#prerequisites
The host networking driver only works on Linux hosts, and is not supported on Docker for Mac, Docker for Windows, or Docker EE for Windows Server.
You can try to use https://docs.docker.com/toolbox/

NRPE remote host setup on amazon ec2

I have been trying to monitor a remote server using Nagios-Nrpe.
The remote host is the Amazon Ec2 instance where I have installed npre daemon on xinetd.
I have added my nagios server IP to "only-from" property in /etc/xinet.d/nrpe file.
I have added the entry in /etc/services.
I have made changes in iptables also.
I have added an entry for TCP port 5666 in my security group too.
These commands work properly:
$ netstat -at | grep nrpe
$usr/local/nagios/libexec/check_nrpe -H localhost
I have setup the nagios server and nrpe_check plugin on my local machine.
But whenever I am doing:
/usr/local/nagios/libexec/check_nrpe -H <"amazon-ec2-IP-address">
I get the following error:
connect to address <"amazon-ec2-IP-address"> port 5666: Connection refused ......
connect to host <"amazon-ec2-IP-address"> port 5666: Connection refused
I have tried making the nrpe client on another linux on my LAN and the command worked, but not for Amazon Ec2.
If anyone has the solution for this issue, please do share ASAP.
Make sure you have,
Opened up port 5666 in the Security Group linked to the EC2-instance.

How should my local server communicate with an EC2 server?

I have a node.js server running on ec2. I'd like for that server to automatically push data to another node.js server that is running on my laptop.
What is the best way to do something like this?
You could use a service like showoff.io to create an entry point to access your local laptop, or you could just create an SSH tunnel by running this command on your laptop:
ssh -R port:localhost:remoteport ec2-host
That will allow port on the loopback interface of your EC2 server to connect to remoteport on your laptop.
Then just modify your code to connect to the node.js program running on your laptop via the IP of 127.0.0.1 and port of port.
You could have the EC2 node.js call a function from the local node.js, and pass the data as variables

Problem with access to Mongodb on Amazon EC2

i've got another question for you.
I have Amazon EC2 instance with mondodb installed.
It works great except one thing - i can't access (connect to) it from outside (my PC).
I think the problem with Security Groups. It's some sort of default firewall.
Does anyone know how to configure EC2 instance to have access to mongodb?
Thanks in advance.
Think carefully before doing this. If you open the ports, make sure you restrict the IP numbers that can access it, otherwise anyone will be able to access your database. You can enable authentication in MongoDB, but it's not particularly safe, just a username and password. You should not have your database open to the internet, it is not a good idea.
A better way than opening up ports in the EC2 firewall is to open an SSH tunnel an forward the port, this makes sure that only you can access the database, and only while the SSH tunnel is active.
Open up a new terminal and run this command (replacing user and host with the user you use when SSH'ing to your server and the name of the server):
ssh user#host -N -L 27017:127.0.0.1:27017
The command will forward the port 27017 on your computer to the same port on the server. To connect to the MongoDB instance simply run mongo in a terminal (if that doesn't work, try mongo --host 127.0.0.1 or even mongo --host 127.0.0.1 --port 27017).
If you run MongoDB on your local machine you will have to change the first port, since the local server is already using it. In that case run this command instead:
ssh user#host -N -L 27018:127.0.0.1:27017
and then connect with
mongo --port 27018
(possibly adding --host 127.0.0.1 if it doesn't work).
When you're done working with the database, exit mongo and press ctrl-C in the terminal with the SSH command.
You need to add a security group exception for the port 27017 if you are using default config for you to access it from outside. For security group configuration, please check the amazon EC2 documentation. And if you are using a different port on Mongo, change the security group port accordingly.
--Sai
Is your EC2 instance a Windows server by any chance? If so, in addition to EC2's Security Groups you also need to configure Windows Firewall to allow the incoming connection.
Go To Administrative Tools, Windows Firewall with Advanced Security, and configure a new Rule that allows incoming connections on port 27017 (the default mongo port) or whatever port you've chosen.

Resources