Unable to connect to MongoDB on EC2 - mongodb

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.

Related

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

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.

Not able to connect to mongodb database

Every time if my server restarts by mistake or I stopped and then restart I'm not be able to connect to my mongodb. I get this below error and according to this 27017 port is not open but in my security group I opened this port.
MongoDB shell version v5.0.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect#src/mongo/shell/mongo.js:372:17
#(connect):2:6
exception: connect failed
exiting with code 1```
So, I don't know what I'm doing wrong please help me because I'm facing this issue from very long time and to rid of this I've to always setup new mongodb
You need to define the MongoDB as service and set to automatic start.
On Windows add lines similar to
processManagement:
windowsService:
serviceName: mongod
displayName: MongoDB
description: MongoDB Server
to your configuration file (typically mongod.conf or mongod.cfg), if not already existing.
Then the easiest way to install the service is running
mongod.exe --config <your config file> --install
On Linux (Redhat, Centos, etc.) it works like this.
Copy default service file
/usr/lib/systemd/system/mongod.service to /etc/systemd/system/mongod.service
Customize service file /etc/systemd/system/mongod.service, if needed
Enable the service: systemctl enable mongod
Start the service: systemctl start mongod

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>

Mongod authentication makes GCP VM Unreachable

I'm using GCP VM to run a MongoDB instance.
It works very well, with authentication turned off, but once I turned it on "enabled" by editing
sudo vi /etc/mongod.conf, and then restart service using sudo service mongod restart.
Once I did this, and try to connect to my DB using my credentials, I've got the following error from mongo:
mongo mongodb://<gcp vm statcic ip>:<mongodb static port>
result in:
connecting to: mongodb://<myservererIP>:<mongodbport>
2020-04-04T19:30:36.644+0200 E QUERY [js] Error: couldn't connect to server
<myservererIP>:<mongodbport>, connection attempt failed: SocketException:
Error connecting to <myservererIP>:<mongodbport> :: caused by :: Connection refused :
connect#src/mongo/shell/mongo.js:257:13
#(connect):1:6
exception: connect failed
My VM IP is Static and account in mongo were created according to mongoDB document, they should be fine, even if they were not, I suppose that I should have an "Authorization required" or something, and not connection refused or Network Unreachable.
I've actually found the problem.
In /etc/mongod.conf , I thought that #security: was here for informative purpose , for file structuring, but you actually have to also uncomment it, along with authentication: enable.
Otherwise, mongod will detect the mongod.conf file as corrupted/bad format, and run without it (probably using a default conf).
I went from:
#security:
authorization: enabled
to
security:
authorization: enabled
Probably a very poor mistake from a begginer, but I'll keep it here, maybe it will help someone.

Mongodb is not connecting with external ip address

I want to host the mean stack application in windows server R12 , Application is accessible when i use Private IP address of the server but it is not accessible when I user the external Ip address or the domain name of the server.I tried with bindIp with addition of One more Ip address. It is not working.
It is showing an error as below
MongoDB shell version v4.0.1 connecting to:
mongodb://xxx.xxx.xx.xx:27017/ 2018-08-09T16:53:22.656+0530 E QUERY
[js] Error: couldn't connect to server xxx.xxx.xx.xx:27017, connection
attempt failed: SocketException: Error connecting to
xxx.xxx.xx.xx:27017 :: caused by :: No connection could be made
because the target machine actively refused it. :
connect#src/mongo/shell/mongo.js:257:13 #(connect):1:6 exception:
connect failed
mongod --repair worked for me ,
sudo mongod --repair
sudo mongod
Then open a different tab/terminal:
mongo
you can check this question here