MySQL连接器 - 由主机强行关闭的连接

问题描述 投票:0回答:1

我通过mysql.connect连接到2个数据库

oldCnx = mysql.connector.connect(user='root', password='root', host='127.0.0.1', database='testdb', connect_timeout=10000)

以相同方式连接到远程数据库集(仅使用其他凭据)。在20(或60)分钟后,我得到错误:mysql.connector.errors.OperationalError: 2055: Lost connection to MySQL server at '127.0.0.1:3306', system error: 10054. An existing connection was forcibly closed by the remote host

该怎么办?我是否需要以某种方式扩展超时或在强制关闭连接时重新连接?如果是这样,我该怎么做?

python mysql mysql-connector
1个回答
0
投票

如果要修改超时,可以使用connection_timeout中的connect()参数

在try / except块中使用cnx.reconnect(attempts=1, delay=0)也可以重新连接。

© www.soinside.com 2019 - 2024. All rights reserved.