Python MySQLConnector打开连接需要一分钟以上的时间

2024-05-16 23:10:03 发布

您现在位置:Python中文网/ 问答频道 /正文

MySQL Connector for python打开到数据库的连接需要>;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秒的门槛,它仍然超时。你知道吗

最后显示的日志是“日志信息('atteming to connect')”在它尝试打开到SQL Server的连接之前。“Connected successfully”不会记录,也不会捕获任何错误。你知道吗

有没有人能澄清一下这种联系是需要这么长时间,还是更可能的选择,指出我错在哪里?你知道吗

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


Tags: theto函数aws数据库sqlsecretconnect
1条回答
网友
1楼 · 发布于 2024-05-16 23:10:03

AWS Lambda函数本身需要一些时间来准备和执行,即使Lambda处于热备用模式。设置超时时请考虑这个事实。尝试增加超时时间,您可以将超时时间增加为300秒,然后重试。你知道吗

相关问题 更多 >