python - 使用 PyMySQL 建立与服务器的连接时遇到问题 [WinError 10054]

标签 python mysql python-3.x pymysql

对于 Python 和 SQL 来说都是新手,所以请原谅我的新手。我们公司有一台服务器,我正在尝试通过一些 python 代码连接到该服务器。我有正确的 IP 地址和登录凭据,并且可以在 SQL Server Management Studio 中连接到它,没有任何问题,但我无法通过 PyCharm 的数据库功能或通过我的代码连接到它。我正在运行 python 3.6,如果这有什么区别的话。

这是我正在运行的代码(为了安全起见,我省略了服务器 IP 地址和登录凭据):

    import pymysql

    db = pymysql.connect(host='server ip', 
                         port=1433, 
                         user='username', 
                         passwd='password', 
                         db='Development DB')

    cursor = db.cursor()        
    cursor.execute("SELECT VERSION()")
    data = cursor.fetchone()
    print("Database version : %s" % data)
    db.close()

当我运行代码时,大约 2 分钟没有任何反应后,我收到此错误:

Traceback (most recent call last):
  File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 1021, in _read_bytes
    data = self._rfile.read(num_bytes)
  File "C:\Program Files\Python35\lib\socket.py", line 575, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/aubrey_s/PycharmProjects/Drawings_Converter/Drawings_Converter.py", line 5, in <module>
    db = pymysql.connect(host='ip address', port=1433, user='username', passwd='password', db='Development DB')
  File "C:\Program Files\Python35\lib\site-packages\pymysql\__init__.py", line 90, in Connect
    return Connection(*args, **kwargs)
  File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 706, in __init__
    self.connect()
  File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 931, in connect
    self._get_server_information()
  File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 1245, in _get_server_information
    packet = self._read_packet()
  File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 987, in _read_packet
    packet_header = self._read_bytes(4)
  File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 1029, in _read_bytes
    "Lost connection to MySQL server during query (%s)" % (e,))
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query ([WinError 10054] An existing connection was forcibly closed by the remote host)')

据我了解,似乎已建立连接,但服务器在一段时间后强制将其关闭。谁能给我更多关于问题可能是什么的见解?提前致谢!

最佳答案

正如我在 another post 中所写的那样,您可能想尝试使用 pymssql ,以防您的数据库不是 MySQL 数据库而是 Microsoft SQL 数据库。

关于python - 使用 PyMySQL 建立与服务器的连接时遇到问题 [WinError 10054],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44189255/

相关文章:

python - 如何使用Spidermon进行Scrapy历史输出比较

python - 无法识别 URL 重定向

php - 不存在实体的 Doctrine ORM 默认值

python - 没有名为 'model_utils' 的模块

python-3.x - Pyspark中的HDFS文件存在检查

python - 三元运算符在Python中是如何实现的

php - 处理 php 形式的撇号时遇到问题

php - MySQL 类似的 2 列需要从第一列开始数据

python - 如何打乱列表,以便每个特定长度的子列表都有唯一的项目?

python - Pillow (PIL) 到 QImage 的转换 -> python.exe 已停止工作