Python MySQL-Connector需要一分钟多的时间才能打开连接

时间:2019-01-03 16:44:21

标签: python mysql aws-lambda mysql-python

用于python的MySQL连接器需要1分钟以上的时间才能打开与数据库的连接。

我正在编写一个AWS Lambda函数,以简单地从AWS Secret Manager中提取当前密码,然后使用该密码打开与SQL数据库的连接并运行查询以获取一些测试数据。

secret = {
        'user': (responseDict['User ID']),
        'password': (responseDict['Password']),
        'host': (responseDict['Data Source']),
        'database': (responseDict['Initial Catalog']),
        'raise_on_warnings': True
}

def connect_database(secret):
    # Creates a client connection to the database, using the secret 
    log.info('Attempting to connect')
    try:
        dbClient = mysql.connector.connect(**secret)

    except mysql.connector.Error as err:
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            logger.error('ERROR: Failed to auth with the SQL Database.')
        elif err.errno == errorcode.ER_BAD_DB_ERROR:
            logger.error('ERROR: Database does not exist.')
        else:
            logger.error('ERROR: Unkown error when connecting to database')
            logger.error(err)

    logging.info('Connected succesfully')
    return dbClient

当我第一次运行lambda函数时,由于达到3秒的超时阈值而失败,因此我将超时时间增加到60秒,并在脚本的各个位置放置了一些信息日志,以查找该位置的时间出来。令我惊讶的是,它的阈值为60秒,仍然超时。

最后一个显示的日志是在试图打开与SQL Server的连接之前的“ log.info('尝试连接')”。 “已成功连接”不会记录,也不会捕获任何错误。

任何人都可以弄清楚连接是需要花这么长时间还是更可能的选择,指出我出了问题的地方?

编辑:这最终与我不知道的网络问题有关,谢谢!

1 个答案:

答案 0 :(得分:0)

AWS Lambda函数本身需要花费一些时间才能准备就绪并执行,但是lambda处于热备模式。设置超时时,请考虑这一事实。尝试增加超时,您可以将超时增加为300秒,然后尝试。