Configuring and connecting to remote MongoDB from Python.

Off late I had to install MongoDB on my Ubuntu 18.04 for one of my side projects and it took quite some time setting it up since I was not much familiar with it before and also I could not find good documentation on the internet regarding this, So I have decided to put it all in an article so that it would be easy for others to do it easily.

If you are not familiar with the installation of MongoDB, kindly go through this link https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

Well, in this article I will explain how to create users, enable authentication, opening up MongoDB access to all IPs and connecting to a remote MongoDB from python code in a simple way without any extra concepts added, let’s get started :-)

1. Creating a user

You need to have at least one user to enable the authentication, with which you gonna login later on.

a) First, ssh into your MongoDB server through PuTTY by entering MongoDB server IP address and the port as 22.

b) Enter the command mongo to enter into the MongoDB shell. Since there is no authentication set up yet, it will allow you directly without any authnz checks.

c) Now switch to the admin DB and create a user with the below command.

use admin
db.createUser(
{
user: "iqbalcrat",
pwd: "secretpassword",
roles: [ "root" ]
}
)

d) You can also create a user that is specific to a database and has read-write privileges, as below.

use mydatabase
db.createUser(
{
user: "dbuser",
pwd: "sepassword",
roles: [
{ role: "readWrite", db: "mydatabase" }
]
})

2) Enable auth and allow all IPs to access MongoDB

Since we have set up our user it’s time to enable the auth, for that go to the MongoDB config file which is located in the below location,

/etc/mongod.conf

Edit the above file with vi or nano editor, scroll down to #security: stanza and uncomment it, then paste the below line under it

security:
authorization: 'enabled'

Now since you have enabled the auth, it’s time to open the MongoDB access to the world so that you can access it from anywhere.

For that, go to # network interfaces stanza in the same /etc/mongod.conf file and change the bindip parameter from 127.0.0.1 to 0.0.0.0

# network interfaces
net:
port: 27017
bindIp: 127.0.0.1 <- change this IP to 0.0.0.0

and that’s it, you have enabled the MongoDB for remote access.

NOTE: Please never set this before enabling the auth, otherwise anyone from the internet can access your DB without any authentication.

Now restart your MongoDB daemon to apply these changes

sudo service mongod restart

from now on if you need to login to mongo shell, you need to provide username and password instead of just typing mongo,

mongo -u username -p password --authenticationDatabase admin mydatabase

3) Connecting to remote MongoDB from python code

Here I will be using PyMongo to connect to MongoDB from python code.

here is how the code goes like,

from pymongo import MongoClient

client = MongoClient("mongodb://username:password@ipaddress:27017")

db = client["db_name"]

# Now you can perfom any CRUD operations on the DB

Here is the documentation link to pymongo lib for more information https://pymongo.readthedocs.io/en/stable/

Hope you find this helpful and if you get stuck anywhere in the configuration, do comment below and I will try to help you out :-)

Jack of all Trades. Engineer.

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