Not able to connect to mongodb running in VM (virtual Machine) from local system - mongodb

I created an AWS EC2 (ubuntu-20.04) instance and setup a mongo server as per https://www.mongodb.com/docs/v4.4/tutorial/install-mongodb-on-ubuntu/
(mongo server is running in default port 27017 and not secured by any username/password)
I am able to connect to it from within the VM (after connecting to VM by SSH), by running the terminal command:
mongo
I have configured EC2 instance's Security Group's inbound rule to access port 27017 from my local system's IP.
however when I try to connect to it from my local system's terminal by command:
mongo --host "<public_ip_of_vm>:27017"
Its throwing error:
connecting to:
mongodb://<public_ip_of_vm>:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server <public_ip_of_vm>:27017, connection attempt
failed: SocketException: Error connecting to <public_ip_of_vm>:27017 :: caused
by :: Connection refused : connect#src/mongo/shell/mongo.js:374:17
#(connect):2:6 exception: connect failed exiting with code 1
Not sure what's causing this issue, I have checked the ufw rules too.
port 27017 is open.

Did you modify ?
/etc/mongod.conf
Have a look at this tutorial, especially the step 2. Maybe this helps
https://ianlondon.github.io/blog/mongodb-auth/

Connection refused means you probably do not have a firewall problem. Connection timeout indicates a firewall issue.
Since you can connect locally via localhost, the error indicates that the mongo process is only listening on localhost.
Edit the file /etc/mogod.conf. The interesting line is bindIp.
It should look like this for IPv4 only:
bindIp: 0.0.0.0
If you have IPv6 enabled
bindIp: ::,0.0.0.0
Warning: enable authentication first. You might be hacked faster than you might expect.

Related

How can i connect to robomongo in my windows to mongodb installed on amazon ec2 instance?

I have installed mongodb v4.4.2 in my amazon linux 2 (on aws ec2 instance) and installed robomongo in my local windows machine. Now i want to connect to my db on ec2 but i always get this error window from robomongo:
Cannot connect to the MongoDB at <ec2_ipv4>:27017.
Error: Network is unreachable. Reason: couldn't connect to server
<ec2_ipv4>:27017, connection attempt failed: NetworkTimeout: Error
connecting to <ec2_ipv4>:27017 :: caused by :: Socket operation timed
out
These are the things that i have done:
Changed bindIp field in /etc/mongod.conf(in ec2 linux) file to 0.0.0.0 and port already set to 27017
stopped and started the mongod server again.
Exposed the port 27017 in security groups in my aws account with tcp protocol with source set to 0.0.0.0/0
in robomongo created a new connection with address: "ipv4 address of ec2 instance" and port = 27017
Is there anything that i have missed. Why is my connection not being established?
I fixed this by add EC2's private IPv4 as a bind IP in /etc/mongod.conf
so it would look like this
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,<your-ec2-private-ipv4>

Mongo accepting connection but compass fails to connect

Compass fails to connect to MongoDb Server although I can see that the connection is accepted in the console and while mongo is set to listen to the right ip and port. (0.0.0.0 27017)
I have setup MongoDb in an LXD container (running on CentOs 7) inside a CentOs 7 server and I have successfully setup port forwarding from the host to the container using the following command:
lxc config device add mycontainer mongoport proxy listen=tcp:0.0.0.0:27017 connect=tcp:0.0.0.0:27017
while running MongoDb without authentication in the container using the command mongod --bind_ip 0.0.0.0 I can see that when Compass tries to make a connection to the server, the connection is actually accepted:
2019-10-16T07:33:51.008+0000 I NETWORK [listener] connection accepted from 127.0.0.1:41674 #1 (1 connection now open)
However, Compass fails to connect as it shows the following message:
Server selection timed out after 10000 ms
I am not running MongoDb with any particular configuration, and since the connection is accepted, what could go possibly wrong and cause this problem? what is the solution?
Update:
I tried disabling SElinux but it seems it has nothing to do with it.

Unable to connect to MongoDB on EC2

Unable to connect MongoDB (Version 4.0.5) on EC2 (Windows Server 2018)
I have tried all the options.
1) Modify mongod.cfg with
net:
port: 27017
bindIp: 0.0.0.0
bindIpAll: true
2) start with mongod --bind_all_ip
3) My security group on EC2 has allowed 27017 port inbound for 0.0.0.0/0.
I'm trying to connect through terminal
mongo mongodb://*********.compute.amazonaws.com:27017
However, I get following exception when connecting through mongo shell
mongodb://e*******.compute.amazonaws.com:27017/?
gssapiServiceName=mongodb
2019-01-21T15:51:11.044-0800 E QUERY [js] Error: couldn't connect
to server *****.compute.amazonaws.com:27017, connection attempt
failed: SocketException: Error connecting to
******.compute.amazonaws.com:27017 (54.244.203.203:27017) :: caused by
:: Operation timed out :
connect#src/mongo/shell/mongo.js:328:13
#(connect):1:6
exception: connect failed
What else I need to do?
Also, it seems my mongodb.cfg has no effect on the server. When I start server with above change in cfg file, I get the warning in server console that the server is bound to localhost, regardless. Starting the server with mongod --bind_all_ip gets rid of that warning.
Firewall was the problem. Need to open port 27017 by running WF.msc from run command on Windows. Spent a whole day trying to figuring out all other options. I still don't know how to get the mongod.cfg working for all ip addresses. I need to run mongod with --bind_all_ip option for that.

Linking mongoDB to elasticbeanstalk application

So I have this problem where my node application that is ran by Docker in elasticbeanstalk cant establish a connection with mongoDB instance that is in another elasticbeanstalk environment under the same application(it deployed and ran with Docker aswell).
I have made sure that both of them are in the same VPC and that the mongoDB instance accepts inbound connections from the security group that the node application is under(inbound connections are accepted on port 27017).
Node application uses mongoDBs private IP in the connection string. Should work because they are in the same VPC.
Somehow the node application cant connect to the mongoDB instance and it throws the same error always.
*** DATABASE ERROR: MongoNetworkError: failed to connect to server [private-ip-here:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND private-ip-here private-ip-here:27017] ***
Can anyone give me advice what am I doing wrong ?
Most likely reason would be not configuring mongod.config file in etc directory.
There would be something like :
net:
bindIp: 127.0.0.1
Comment the bindIP or replace it with your server's IP Address.

Compute Engine With VPC Firewall Open Could Not Connect To Mongo Port 27017 Remotely

I installed MongoDB v3.6.4 server. I have the following settings in Google Compute Engine:
Configuration
In /etc/mongod.conf file:
net:
port: 27017
bindIp: 0.0.0.0
security:
authorization: enabled
I also did the following:
Removed mongod.lock
Created a firewall rule in VPC for both tcp:27017 and udp:27017 to be open.
Connection:
I can connect locally by:
mongo 127.0.0.1:27017/database -u myusername -p
But, I can not connect remotely using an IP address or domain by:
mongo my.ip.com:27017/database -u myusername -p
mongo <IP ADDRESS>:27017/database -u myusername -p
Error
It gives me the error:
[thread1] Failed to connect to <IP ADDRESS>:27017 after 5000ms milliseconds, giving up.
[thread1] Error: couldn't connect to server my.ip.com:27017, connection attempt failed : connect#src/mongo/shell/mongo.js:237:13
Other Things I've Tried
switched back and forth from different server versions from micro, small, and 1vCPU (thinking it was the processor)
Changed configurations over and over of the net and security
Added a static IP address.
Checked my other servers with the same setting which worked and copied from it.
Checked the logs and it does not give me anything after remotely connecting.
Checked my firewall name, which is default-mongodb for tcp:27017 and udp:27017.
None of the settings in this specific server seem to work.
Other Information
I'm connecting using Mongo client v3.4.9
Did I miss to check something? Why is it not working?
Never mind... I did not enter the range for the VPC Firewall Rule.
It should be 0.0.0.0/0 instead of just 0.0.0.0