How to connect to your remote MongoDB server

I have MongoDB running on my Ubuntu server in Amazon EC2. Since there’s no simple all-in-one tutorial out there explaining how to set up user authentication for Mongo so that you can read and write to your MongoDB server from your laptop, I decided to write one.

If you haven’t installed MongoDB yet, follow the steps at https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/ first.

1. Set up your user

First ssh into your server and enter the mongo shell by typing mongo. For this example, I will set up a user named hardeep and give that user read & write access to the `animals` database.

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

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

Edit your MongoDB config file. On Ubuntu:

sudo vim /etc/mongod.conf

  • Look for the net line and comment out the bindIp line under it, which is currently limiting MongoDB connections to localhost:

Warning: do not comment out the bindIp line without enabling authorization. Otherwise you will be opening up the whole internet to have full admin access to all mongo databases on your MongoDB server!

# 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)

sudo service mongod restart

Make sure you can still log in with mongo while ssh’d into the box.

If anything goes wrong, look at the log: tail -f /var/log/mongodb/mongod.log (note: non-Ubuntu machines will keep the log in another directory…)

Logging in using the mongo shell on your laptop

You can close out of ssh and go back to your local console. To enter the remote Mongo database we just set up, you can use the mongo shell:

mongo -u hardeep -p yourSecretPassword 169.45.23.99/animals

Where 169.45.23.99 is your server’s public IP address.

Now you can read and write within the animals database from your laptop without ssh!

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()

And that’s it!

Hope it help to you. Please give clap 👏

--

--

--

Founder and C.E.O at XpRate.com | Want to help world to grow 🌱

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Organize Files the Lazy Man’s way Using Python

HPSS and Splunk

Player Damage Visual Effect

Programmable Constraint Systems for Bulletproofs

Launch an EC2 instance in AWS Cloud and Configuring webserver to access static files using a web…

Terraform Intro…

My Test Automation Journey

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
Hardeep Singh

Hardeep Singh

Founder and C.E.O at XpRate.com | Want to help world to grow 🌱

More from Medium

Learn how to dockerize a simple application with this quick guide

The Guide to Docker ARG, ENV and .env

How detect slow SQL query in PostgreSQL with EXPLAIN command

How to upload and delete files with AWS S3 using node.js