Esempio n. 1
0
def my_slowquery_kill(db_instance):
    db_id = db_instance[0]
    db_host, db_port = db_instance[1].replace(' ', '').split(':')
    #print "db_id, db_host, db_port" + db_id+db_host+db_port

    db_commconfig = get_setttings("db_commconfig")

    # 获取具体的db_instance 选项kill
    kill_opt = get_setttings("id_" + db_id)

    # 登录db认证信息
    #db_users = json.loads(db_commconfig["db_auth"])
    db_users = settings.DB_AUTH

    # 每个db实例的多个用户维持各自的连接
    db_conns = {}

    # db连接密码解密
    pc = prpcryptec.prpcrypt(KEY_DB_AUTH)
    for db_user, db_pass in db_users.items():
        dbpass_de = pc.decrypt(db_pass)
        try:
            conn = MySQLdb.Connect(host=db_host, user=db_user, passwd=dbpass_de, port=int(db_port), connect_timeout=5, use_unicode=False)
            db_conns[db_user] = conn
            logger.info("connection is created: %s:%s  %s", db_host, db_port, db_user)

        except MySQLdb.Error, e:
            logger.warn('Error %d: %s', e.args[0], e.args[1])
            logger.warn('  User %s may not exist in DB %s . Skip it for you.', db_user, db_host)
Esempio n. 2
0
def db_reconnect(db_user, db_id):
    db_pass = settings.DB_AUTH[db_user]
    pc = prpcryptec.prpcrypt(KEY_DB_AUTH)

    db_instance = get_setttings("db_info", db_id)
    db_host, db_port = db_instance.replace(' ', '').split(':')

    db_conn = None

    while not db_conn:
        try:
            logger.warn("Reconnect Database %s: host='%s', user='******'Error %d: %s', e.args[0], e.args[1])
                logger.warn("Reconnect Database %s: host='%s', user='%s, port=%s",
                            db_id, db_host, db_user, db_port)
                db_conn = MySQLdb.Connect(host=db_host, user=db_user, passwd=pc.decrypt(db_pass), port=int(db_port),
                                          connect_timeout=5, use_unicode=False)

        except Exception as inst:
            print "Error %s %s" % type(inst), inst.args.__str__()
Esempio n. 3
0
def my_slowquery_kill(db_instance):
    db_id = db_instance[0]
    db_host, db_port = db_instance[1].replace(' ', '').split(':')
    #print "db_id, db_host, db_port" + db_id+db_host+db_port

    db_commconfig = get_setttings("db_commconfig")

    # 获取具体的db_instance 选项kill
    kill_opt = get_setttings("id_" + db_id)

    # 登录db认证信息
    #db_users = json.loads(db_commconfig["db_auth"])
    db_users = settings.DB_AUTH

    # 每个db实例的多个用户维持各自的连接
    db_conns = {}

    # db连接密码解密
    pc = prpcryptec.prpcrypt(KEY_DB_AUTH)
    for db_user, db_pass in db_users.items():
        dbpass_de = pc.decrypt(db_pass)
        try:
            conn = MySQLdb.Connect(host=db_host,
                                   user=db_user,
                                   passwd=dbpass_de,
                                   port=int(db_port),
                                   connect_timeout=5,
                                   use_unicode=False)
            db_conns[db_user] = conn
            logger.info("connection is created: %s:%s  %s", db_host, db_port,
                        db_user)

        except MySQLdb.Error, e:
            logger.warn('Error %d: %s', e.args[0], e.args[1])
            sys.exit(-1)
Esempio n. 4
0
def db_reconnect(db_user, db_id):
    db_pass = settings.DB_AUTH[db_user]
    pc = prpcryptec.prpcrypt(KEY_DB_AUTH)

    db_instance = get_setttings("db_info", db_id)
    db_host, db_port = db_instance.replace(' ', '').split(':')

    db_conn = None

    while not db_conn:
        try:
            logger.warn("Reconnect Database %s: host='%s', user='******'Error %d: %s', e.args[0], e.args[1])
                logger.warn(
                    "Reconnect Database %s: host='%s', user='%s, port=%s",
                    db_id, db_host, db_user, db_port)
                db_conn = MySQLdb.Connect(host=db_host,
                                          user=db_user,
                                          passwd=pc.decrypt(db_pass),
                                          port=int(db_port),
                                          connect_timeout=5,
                                          use_unicode=False)

        except Exception as inst:
            print "Error %s %s" % type(inst), inst.args.__str__()