gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:55:04 26 4
gpt4 key购买 nike

对于 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com