-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongoconnection.py
29 lines (23 loc) · 925 Bytes
/
mongoconnection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import os
from pymongo import MongoClient
from dotenv import load_dotenv
from urllib.parse import quote_plus
# Load environment variables from the .env file
load_dotenv()
# Retrieve MongoDB connection details from environment variables
mongo_db = os.getenv('MONGO_INITDB_DATABASE')
mongo_user = os.getenv('MONGO_INITDB_ROOT_USERNAME')
mongo_password = os.getenv('MONGO_INITDB_ROOT_PASSWORD')
# URL-encode the username and password
encoded_user = quote_plus(mongo_user)
encoded_password = quote_plus(mongo_password)
# Construct MongoDB connection string
connection_string = f"mongodb://{encoded_user}:{encoded_password}@localhost:27017/{mongo_db}?authSource=admin"
try:
# Initialize MongoDB client
client = MongoClient(connection_string)
# Check connection
client.admin.command('ping')
print("Connection to MongoDB successful!")
except Exception as e:
print(f"Failed to connect to MongoDB: {e}")