Created
September 30, 2024 13:28
-
-
Save davidt-gh/a1c5bb624aaa4de6f7904aa6aae4efd7 to your computer and use it in GitHub Desktop.
python script to test connection into mongo db
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import pymongo | |
from pymongo.errors import ConnectionFailure | |
def connect_to_mongo(connection_string): | |
try: | |
# Create a MongoDB client | |
client = pymongo.MongoClient(connection_string) | |
# The ismaster command is cheap and does not require authentication | |
client.admin.command('ismaster') | |
print("MongoDB connection successful!") | |
except ConnectionFailure as e: | |
print(f"MongoDB connection failed: {e}") | |
if __name__ == "__main__": | |
if len(sys.argv) != 2: | |
print("Usage: python mongo_connect.py <mongo_connection_string>") | |
sys.exit(1) | |
connection_string = sys.argv[1] | |
connect_to_mongo(connection_string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment