How to connect to remote mongoDB via ssh (via Sacred)? connection refused

Solution 1:

Your error says pymongo is looking for mongod on localhost:27017

pymongo.errors.ServerSelectionTimeoutError: localhost:27017

But it isn't there, you've forwarded it to localhost:6666. The connection string in your code must have a hardcoded default you'll need to edit.

Or, if nothing is running on localhost:27017 you can tunnel directly:

ssh -fN -i ~/.ssh/sacred_key-pair.pem -L 27017:localhost:27017 [email protected]

Other useful commands are

  • List ssh processes

ps aux | grep ssh

  • Which process is using a port (27107 in this case)

sudo netstat -lnpt | awk '$4 ~ /:27107/ {sub(/\/.*/, "", $7); print $7}'

  • Free up a port by killing the process using it

kill <pid>

Also bind to local host, using 0.0.0.0 is making mongodb available to anyone anywhere that can reach the server.

net:
  port: 27017
  bindIp: 127.0.0.1