首页 > 解决方案 > Python MySQL-Connector 无法在 Windows VPS 上间歇性连接

问题描述

我正在使用 Windows VPS 抓取数据。我正在使用 Python MySQL-Connector 将这些抓取的数据上传到 Linux 专用服务器。

import mysql.connector

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

大约 25% 的时间会失败,或者需要 20 多秒。当它失败时,我得到一个 10060 错误。问题更可能出在我的 Linux 服务器上,还是更可能出在我的 Windows VPS 上?帮助由衷感谢。这是错误消息:

2055: Lost connection to MySQL server at '[IP]:3306', system error: 10060 A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

标签: pythonmysqllinuxservermysql-connector

解决方案


查看错误消息,我怀疑这是由端口被阻塞引起的(MySQL 服务器的默认 TCP/IP 端口是 3306)。

检查 3306 端口是否忙:

netstat -tulpn | grep 3306

然后在再次运行您的应用程序之前尝试释放该端口。


推荐阅读