首页 > 解决方案 > Python MySQL 连接器超时

问题描述

使用 Python MySQL 连接器将记录插入 MySQL 数据库。流程在大约 98% 的时间里有效。我从服务器收到间歇性超时响应。其他时候脚本只是挂起,什么也没有发生,这是最糟糕的情况。

我可以使用线程超时/终止进程吗?

我可以在执行或提交语句上设置超时吗?

import mysql.connector

try:
    mydb = mysql.connector.connect(host="...", user="...", password="...", database="...")
    mycursor = mydb.cursor()

except Exception as e:
        print(e)

#Seems to always work up to this point

sql = "INSERT INTO test (name,tagline,location,experience) VALUES (%s, %s, %s, %s)"
val = ('test', 'test', 'test', 'test')

try:
    mycursor.execute(sql, val)
    mydb.commit()

except Exception as e:
        print(e)
mycursor.close()
mydb.close()  

标签: pythonmysqlmysql-pythonmysql-connector

解决方案


推荐阅读