Watch keynotes and sessions from MongoDB.live, our virtual developer conference.

Installing MongoDB server via Python

Hi everyone,

Here is my project :

I’m creating a Python application, with a user interface. This application has to be connected to a MongoDB database, in ordre to save datas. My aim is to create an installer for this app, so that anyone can install it and access to a local MongoDB database, without having to download or install MongoDB previously.

I would like to put all these steps inside my app : installing a mongodb server and connecting the app to this server.

Is it possible ?
And if yes, how can I do it ?
I didn’t manage to find a way to do it !

Thank you.

Hi Claire,

It may be possible since Python can execute shell command using various methods (popen, subprocess, etc.). However, installing any server is usually more involved than just running the executable, and some people may consider auto-installing a server they don’t know about to be a little invasive.

Instead, how about mentioning that your app depends on a working MongoDB server, and installing the server should be the first step in installing your app?

Another idea is to use a cloud-based MongoDB deployment e.g. Atlas as your app’s database, so you’ll know it’s always available and you wouldn’t depend on the user to be able to properly install the MongoDB server by themselves.

Best regards,
Kevin

1 Like

Hi Claire,

You will need to roll your own custom solution to achieve something like this. Your solution will need to do 2 things:

  1. Download MongoDB binaries for the platform you are on. The m tool (https://github.com/aheckmann/m) does exactly this (in addition to managing multiple versions on the same machine simultaneously which you don’t need to do) so studying its source code would be a good place to start.
  2. Starting and configuring a MongoDB cluster using the downloaded binaries. If you only want to create a standalone MongoDB instance (no replication/high-availability) then this step is relatively simple. For any other use case, you need some code to orchestrate the cluster you will end up using. You can look at the mongo-orchestration project (note: this project is not supported by MongoDB) for an example of a project that does this. Procedures for deploying a replica set (and other configurations are provided in the MongoDB Manual): https://docs.mongodb.com/manual/tutorial/deploy-replica-set/

Overall, I think building a stable solution that does what you like will take significant effort and will be hard to maintain (URLs of MongoDB binaries might change, steps required to configure a cluster might change, etc.). I think Kevin’s suggestion of requiring the user to have a working MongoDB deployment is a better idea.

-Prashant

1 Like