Hi all,
When I try running a script on either VSCode or Pycharm to try establish a connection, it keeps timing out after 2000 ms. Furthermore, this is what it returns:
pymongo.errors.ServerSelectionTimeoutError
Find below the code which is causing the issue:
def create_db_connection():
"""
Creates a connection to the MongoDB database using credentials stored in the 'credentials.env' file.
The function reads the database username, password, host, and port from environment variables,
constructs a MongoDB connection string, and establishes a connection to the 'ernie' database.
Returns:
pymongo.database.Database: A database connection object that allows interaction with MongoDB.
Raises:
Exception: If the database connection fails due to incorrect credentials or network issues.
"""
dotenv.load_dotenv(path.dirname(__file__)+"/credentials.env")
connection_string = "mongodb://{}:{}@{}:{}".format(
parse.quote_plus(str(getenv("MDB_USER"))),
parse.quote_plus(str(getenv("MDB_PASSWORD"))),
getenv("HOST"),
getenv("MDB_PORT")
)
mongo_client = MongoClient(connection_string)
db_access = mongo_client["ernie"]
return db_access
"""