Skip to content

Instantly share code, notes, and snippets.

@davidt-gh
Created September 30, 2024 13:28
Show Gist options
  • Save davidt-gh/a1c5bb624aaa4de6f7904aa6aae4efd7 to your computer and use it in GitHub Desktop.
Save davidt-gh/a1c5bb624aaa4de6f7904aa6aae4efd7 to your computer and use it in GitHub Desktop.
python script to test connection into mongo db
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