How to connect to your remote MongoDB server

1. Set up your user

use animalsdb.createUser({
user: 'hardeep',
pwd: 'yourSecretPassword',
roles: [{ role: 'readWrite', db:'animals'}]
})

2. Enable auth and open MongoDB access up to all IPs

  • Look for the net line and comment out the bindIp line under it, which is currently limiting MongoDB connections to localhost:
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 <- update this line
  • Scroll down to the #security: section and add the following line. Make sure to un-comment the security: line.
security:
authorization: 'enabled'

3. Open port 27017 on your EC2 instance

  • Go to your EC2 dashboard: https://console.aws.amazon.com/ec2/
  • Go to Instances and scroll down to see your instance’s Security Groups. Eg, it will be something like launch-wizard-4
  • Go to Netword & Security -> Security Groups -> Inbound tab -> Edit button.
  • Make a new Custom TCP on port 27017, Source: My IP

4. Last step: restart mongo daemon (mongod)

Logging in using the mongo shell on your laptop

Using pymongo with your remote MongoDB server

import pymongoclient = pymongo.MongoClient("mongodb://hardeep:yourSecretPassword@169.45.23.99/animals")# defaults to port 27017, db = client.animals
# print the number of documents in a collection
#
sameURL (MongoClient) can help to connect via NODEJS.
print db.animals_collection.count()

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store