Making MongoDB remotely available.




Summary

Here I show how to make your MongoDB accessible from a different server / computer/ instance, without using ssh.

Motivation

I have been using MongoDB to store and query some databases. This databases has been shared with other people and in most of the cases I have managed to use it as localhost or using ssh. However when using Docker it is easy to run into problems when using ssh. It is common to access other databases such as MySQL.

For example in to use MySQL in Python you could have something like this:
import pymysql.cursors

# Connect to the database
connection = pymysql.connect(host='localhost',
                             user='user',
                             password='passwd',
                             db='db',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)

In the same line you could query your MongoDB using PyMongo
    import pymongo
    from pymongo import MongoClient
    client = MongoClient('localhost',27017)
    db = client[database_name]

where 'localhost' is just your machine, or '127.0.0.1'. Also you could use a Mongo URI . For example:
client = pymongo.MongoClient("mongodb://tom:jerry25K@35.178.145.134:27017"),
where you are saying to pymongo,  the host: 35.178.145.134, the user: tom, the password: jerry25K.

Fair enough, but how to have this or similar into place?. This is the key point here. So you can use your MongoDB from a remote computer.

Two cases


We should discuss first a little bit about security. Do you want to have a user with the password? Do you want to open the ports of you MongoDB server?

Probably the most secure here is to grant access from some particular IPs and to have a user with a password. However I will cover both so you choose. For example, you may not have permissions to create new users.


Open ports and NO user with password
You should grant access from the database and from the server.

  1. From the database. MongoDB by default can only be accessible from localhost. There are different ways to overcome that. I suggest to modify the configuration file.
    1. sudo vim /etc/mongod.conf
    2. Go to "network interfaces", the default will be 127.0.0.1, just change that to 0.0.0.0
With this you are saying to MongoDB to listen to every available network interface [Ref].

    1. After that, please restart you MongoDB: sudo service mongod restart.




Now you should open the port 27017 (where MongoDB is working to external access).  Usually when you query an IP you may not have access so you need to have access first to the database. If you are in AWS you can follow the next steps, [from here].
  • 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: Anywhere, 0.0.0.0/0
Now you should be able to query your database without problems. To check it you could use Robo3T. Here a snapshot from a query using Python 2.
JOB DONE!
Open ports and WITH user with password

This option include the previous one. So you should do the previous one before you continue here.

Go back to the configuration file and allow for authorization.
  1. sudo vim /etc/mongod.conf
  2. Create another user. Here I put two different scenario. A simple user or a SuperUser:
    1. Create a user with access to a database, borrowed from https://ianlondon.github.io/blog/mongodb-auth/: 
    2. Superuser: here the steps https://stackoverflow.com/a/34634554/7127519. I chose this one because I amp playing around with an old snapshot I will destroy inmediately, so noone care really.
    3. You could create many different types of user, here some examples https://docs.mongodb.com/manual/reference/method/db.createUser/.
  3. Enable Security:
    1. Before, you may find something like this or just empty 
    2. After, you should uncomment and add: 
    3. Now you are enable the authorization. 
  4. Do not forget to restart to activate the changes by restarting the database: sudo service mongod restart.

Let see, as before you could check using Robo3T, probably easier.:
So there you see two of my databases.
Job Done!

My environment


I have been using Ubuntu 16.04. I have check this in a local installation and in AWS. Python 2.

References


  1. A post similar to this one. However I also talk about the possibility of not use or create a user and there is a type here, in this post the author suggest to remove the bindIP while here just to change to 0.0.0.0, in my case the first option didn't work. Something share with other people as you can read in the comments. Nevertheless a excellent post:  https://ianlondon.github.io/blog/mongodb-auth/ 
  2. If you want to create roles withing you MongoDB, here there is a excellent post to help you. It distinguis also depending of the version of your MongoDB: https://stackoverflow.com/a/34634554/7127519 
  3. How to create your users in MongoDB, great post: https://stackoverflow.com/a/34634554/7127519 . 



Comentarios

Entradas populares de este blog

Reflecting about SIR models and some examples

Adicction and Decision Making: a brainy view.