def is_db_server_up(mongo_url): client = MongoClient(mongo_url, serverSelectionTimeoutMS=100) try: client.server_info() return True except ServerSelectionTimeoutError: return False
def get_db_version(mongo_url): """ Return the mongo db version :param mongo_url: Which mongo to check. :return: version as a tuple (e.g. `(u'4', u'0', u'8')`) """ client = MongoClient(mongo_url, serverSelectionTimeoutMS=100) server_version = tuple(client.server_info()["version"].split(".")) return server_version
def main(ip='*', ip_db='*'): global url, collection url = "tcp://{}:9002".format(ip) app.config['MONGO_DBNAME'] = 'users' app.config['MONGO_URI'] = 'mongodb://' + ip_db + ':27017/users' dbconnect = False while not dbconnect: conn = MongoClient('mongodb://' + ip_db + ':27017/', serverSelectionTimeoutMS=5000, connectTimeoutMS=200000) try: conn.server_info() # force connection # Database db = conn.users # Created or Switched to collection names: my_gfg_collection collection = db.user_collection dbconnect = True except ServerSelectionTimeoutError as e: if DEBUG: print(e) print("Could not connect to MongoDB", flush=True) app.run(debug=True, host='0.0.0.0', port='5002')
from flask_pymongo import MongoClient from ..config import config try: mongo = MongoClient( host=config['DB_HOST'], #db host port=int(config['DB_PORT']), #db port serverSelectionTimeoutMS=1000) db = mongo.config['DB_NAME'] #db_name mongo.server_info() print('Connected to DB') except Exception as err: print("ERROR - Cannot connect to DB") print(err)