MongoDB Errorno. 61 (ServerSelectionTimeoutError)

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

 """

Hi @Soumyaraj_Chatterjee, is this a local MongoDB instance or is it running on Atlas? If it is running on Atlas, you need to use mongodb+srv:// instead of mongodb://. If it is running locally, are you able to connect using MongoDB shell or compass?

yes, so im running this on my mac terminal within PyCharm

Can you tell more more about how you created the MongoDB cluster?