【问题标题】:Kill connections to handle 1205 (HY000): Lock wait timeout exceeded; try restarting transaction?终止连接到句柄 1205 (HY000):超过锁定等待超时;尝试重启交易?
【发布时间】:2022-01-24 06:37:12
【问题描述】:

我在 Python 中使用 MySQL.connector 来构建数据库。我突然遇到了这个错误信息。

我从这里的其他帖子中了解到,这与太多的连接有关。如果我运行下面的代码,我会得到这个作为回报。


pd.read_sql('SELECT * FROM `information_schema`.`innodb_trx` ORDER BY `trx_started`', mydb)

¨

我还尝试将连接数增加到 500。但这并没有改变任何事情。

cursor.execute("set global max_connections = 500")
mydb.commit()

现在我正试图弄清楚这些信息告诉我什么。我应该终止所有连接并重新开始吗?我可以杀死来自 python 的连接吗?以后如何避免这个问题?

【问题讨论】:

  • 您不需要终止连接来解决锁定超时问题。只需优化锁定查询,使其快速运行并及时提交事务。
  • 有趣 - 我会调查的!

标签: python mysql


【解决方案1】:

这解决了我的问题。

cursor.execute("select concat('kill ',id,';') sleeping_connections FROM information_schema.processlist isp WHERE Command = 'Sleep' and time > 60")
rows = cursor.fetchall()
df_ = pd.DataFrame( [[ij for ij in i] for i in rows] )
df_.rename(columns={0: 'Sleeping Connections'}, inplace=True);

for i in df_['Sleeping Connections']:
    cursor.execute(str(i))

【讨论】:

    猜你喜欢
    • 2021-08-07
    • 2019-06-02
    • 1970-01-01
    • 2015-09-27
    • 2021-01-12
    • 2015-07-18
    • 2013-08-10
    • 1970-01-01
    • 2013-02-05
    相关资源
    最近更新 更多