Python
Learn how to connect to MongoDB databases in Sealos DevBox using Python
This guide will walk you through the process of connecting to a MongoDB database using Python within your Sealos DevBox project.
Prerequisites
- A Sealos DevBox project with Python environment
- A MongoDB database created using the Database app in Sealos
Activating the Python Environment
Before you start, you need to activate the Python virtual environment in your DevBox. Open the terminal within Cursor IDE and run:
You should see your prompt change, indicating that the virtual environment is now active.
Installing Required Packages
In your Cursor terminal, install the necessary packages:
This command installs:
pymongo
: The official MongoDB driver for Pythonpython-dotenv
: A Python package that allows you to load environment variables from a .env file
Connection Setup
Set up the environment variables
First, let's set up the environment variables for our database connection. Create a .env
file in your project root with the following content:
Replace the placeholders with your actual MongoDB credentials from the Database app in Sealos.
Create a database connection module
Create a new file named db_connection.py
with the following content:
This module provides two main functions:
-
get_db_connection()
: This function establishes a connection to the MongoDB database using the credentials stored in the environment variables. It returns the client object if successful, or None if an error occurs. -
close_connection(client)
: This function closes the database connection when it's no longer needed.
Create a test script
Now, let's create a test script to verify our connection and perform some basic database operations. Create a file named test_mongodb.py
with the following content:
This script demonstrates basic CRUD operations:
- Inserting a document
- Finding documents
- Updating a document
- Deleting a document
Running the Test Script
To run the test script, make sure your virtual environment is activated, then execute:
If everything is set up correctly, you should see output indicating successful connection, document insertion, retrieval, update, and deletion.
Best Practices
- Always activate the virtual environment before running your Python scripts or installing packages.
- Use environment variables to store sensitive information like database credentials.
- Close database connections after use to free up resources.
- Use try-except blocks to handle potential errors gracefully.
- Use PyMongo's built-in methods for database operations to ensure proper handling of MongoDB-specific data types.
Troubleshooting
If you encounter connection issues:
- Ensure you've activated the virtual environment with
source ./bin/activate
. - Verify that your MongoDB database is running and accessible.
- Double-check your database credentials in the
.env
file. - Check the MongoDB logs in the Database app for any error messages.
For more detailed information on using MongoDB with Python, refer to the official PyMongo documentation.
Last updated on