Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pymongo connection timeout from remote machine

I have a Bitnami MEAN Stack running on AWS EC2. I'm trying to connect from a remote machine using PyMongo.

from pymongo import MongoClient
conn = MongoClient('mongodb://username:[email protected]:27017/dbname')

but I keep getting an error along the lines of pymongo.errors.ConnectionFailure: timed out

I have edited /opt/bitnami/mongodb/mongodb.conf to supposedly allow external connections by commenting out bind_ip = 127.0.0.1 and uncommented bind_ip = 0.0.0.0 and all permutations of commenting/uncommenting those lines.

I've looked over the web for about 90 minutes now trying different things but without luck!

like image 781
nodebase Avatar asked Jan 10 '23 06:01

nodebase


2 Answers

On the mongoDB server, do the port connection test, and make sure the DB service running well. If not, start the service.

telnet ec2blah.us-east-1.compute.amazonaws.com 27017

On the remote machine, do the port connection test, to make sure there is no firewall issue.

telnet ec2blah.us-east-1.compute.amazonaws.com 27017

If you have issue to connect, you need check security groups on this instance.

Click the ec2 instance name --> Description --> view rules, you should see the ports are opened

If not, create a new security group , such as `mongoDB`, tcp port 27017 should be opened for inbound traffic, then assign to that instance. 

You should be fine to connect it now.

like image 78
BMW Avatar answered Jan 11 '23 19:01

BMW


At the time of start-up of MongoDB, set the bind_ip argument to ::,0.0.0.0

mongod --bind_ip ::,0.0.0.0

Read more in the docs of MongoDB: IP Binding.

like image 34
Ashish Jain Avatar answered Jan 11 '23 19:01

Ashish Jain