Python MySQL-连接器打开一个连接需要超过一分钟

Python MySQL-Connector taking more than a minute to open a connection

MySQL-python 的连接器打开数据库连接的时间超过 1 分钟。

我正在编写一个 AWS Lambda 函数来简单地从 AWS 秘密管理器中提取当前秘密,然后使用该秘密打开与 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 秒的阈值,它仍然超时。

最后显示的日志是 "log.info('Attempting to connect')" 试图打开与 SQL 服务器的连接之前。 'Connected Succesfully' 不会记录,也不会捕获任何错误。

任何人都可以澄清连接是否应该花费这么长时间或更可能的选择,指出我哪里出错了?

编辑:这最终与我没有意识到的网络问题有关,谢谢!

AWS Lambda 函数本身需要一些时间来准备和执行,eventhouh lambda 处于 hotstand by 模式。请在设置超时时考虑这一事实。尝试增加超时,您可以将超时增加为 300 秒并尝试。