MongoDB Connection String | Microsoft Azure

Azure's automatic formatting of connection strings.

What is a MongoDB connection string?

I just created my own little blog and I used mongoDB to manage my data within the blog. I created a MongoDB “Cluster” and specified collections to store User and Post data. To interact with mongoDB via Python I used this python library called pymongo developed by the team at Mongo. The way I connect to my mongoDB cluster is by means of a connection string which usually looks something like this (Depending on whether you are using Atlas or not)

mongodb+srv://myDatabaseUser:D1fficultP%[email protected]/?authSource=admin&replicaSet=myRepl

Let’s talk about the connection string first. The connection strings let’s you initialize your MongoDB client in pymongo:

import pymongo 

mongo_connection_string = "mongodb+srv://myDatabaseUser:D1fficultP%[email protected]/?authSource=admin&replicaSet=myRepl"

client = pymongo.MongoClient(mongo_connection_string)

The functionality and the specifics of the connection string are irrelevant to the point I’m about to discuss here, but if you want to learn more about mongo connection strings in specific check out the documentation.

What can be relevant here is the credentials you set for the connection string. If you’re credentials contain any of these characters

$ : / ? # [ ] @

It is very important to encode them (check out percent encoding) so that mongo can parse them properly.

Securing the Connection String

It is very important to securely store your connection string because anyone who has access to you connection string has access to your database

The way I secured my connection string in my flask development environment was adding the connection string as an environment variable.

The .env file held the environment variable

#/.env

MONGO_CONNECTION_STRING="mongodb+srv://myDatabaseUser:D1fficultP%[email protected]/?authSource=admin&replicaSet=myRepl"

The .gitignore file specifics that git should ignore the .env file.

#/.gitignore

.env

And then finally the we load the environment variable in python as such:

import os
from dotenv import load_dotenv


load_dotenv()

mongo_connection_string = os.getenv("MONGO_CONNECTION_STRING")

This process keeps your mongo connection string hidden.

Azure and its Connection String Formatting?

I deployed my flask web application using Microsoft Azure Web Apps. The process was fairly simple and Azure even set up a CI/CD pipeline via Github Actions.

The first time Azure tried to build my app, it failed. However, that was a simple fix and Azure was having a little trouble setting up the virtual environment because of the way I had set it in my deployment environment. The main problem started after that. When my Web App successfully deployed, pymongo kept throwing

pymongo.errors.InvalidURI: Invalid URI scheme: URI must begin with 'mongodb://' or 'mongodb+srv://' 

This was very confusing. Azure lets you set connection strings within the production environment and there are specific prefixes you have to give to your environment variables in order to load them in.

In Azure, the connection strings have a type and depending on that type you give your environment variables that you load a specific prefix

SQL Database prefix is SQLAZURECONNSTR_

SQL Server prefix is SQLCONNSTR_

MySQL prefix is MYSQLCONNSTR_

Custom prefix is CUSTOMCONNSTR_

At first I did not add the prefix so after I read more of the documentation and then applied the changes to match the format that Azure asked me to, I deployed it again. Still, to no avail. The mongo connection string would simply not work.

Turns out, When you provide a connection string to Azure (or other cloud services) and it gets reformatted with additional backslashes (e.g., turning a string into a string within a string with \ characters), this typically happens due to the way special characters are handled and escaped within the string.

So all I had to do to fix that problem was to go to the Advanced Settings and manually change the json file for the connection string, to remove the additional characters Azure had added. After that I deployed and it successfully worked without throwing any errors at me.

All in all, it took me a couple hours to figure out what was going wrong, I used Azure because in collaboration with the Github Student Developer Pack I get a 100$ of Azure credit for free. I like Azure now.

About the Author

I’m Syed, a sophomore computer science student here at the University of North Texas. I’ve been into programming ever since I was 14 and I love sharing my geeky stories with fellow “geeks

Reply

or to participate.