我使用以下代码连接到我的云主机(注X是实际凭证的占位符)。是的,我已经环顾四周,我没有找到有用的答案。
import pymysql
try:
db = pymysql.connect(host='xxxxxxxx', user='xxxxxx',
password='xxxxxx', database='xxxxxxxx',
port=xx, connect_timeout=60)
except Exception as e:
print(f'Failed connection {str(e)}')我得到了下面的错误
Traceback (most recent call last):
File "C:\Users\THEMBEKILE\Desktop\Projects\Cafe Nalla1\dbcreatetable.py", line 10, in <module>
database="xxxxxxxx", port=xxxxx, connect_timeout=60)
File "C:\Users\THEMBEKILE\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "C:\Users\THEMBEKILE\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 325, in __init__
self.connect()
File "C:\Users\THEMBEKILE\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 598, in connect
self._get_server_information()
File "C:\Users\THEMBEKILE\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 976, in _get_server_information
packet = self._read_packet()
File "C:\Users\THEMBEKILE\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 657, in _read_packet
packet_header = self._read_bytes(4)
File "C:\Users\THEMBEKILE\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 708, in _read_bytes
CR.CR_SERVER_LOST, "Lost connection to MySQL server during query")
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')请注意:所有的凭证都是正确的,我已经使用了这个代码为早期的应用程序,只是一个不同的数据库和相同的供应商,一切都很好。
我可以使用cmd和这个命令连接到DB。更让我困惑的是:Sqlcmd -S xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -d xxxxxxxxxxx -U xxxxxxxxxx -P xxxxxxxx
在这件事上的任何帮助都将受到高度的感谢。
发布于 2021-05-21 19:05:17
在做了一些研究之后,我发现了我的问题。我使用cloudcluster打开了一个SQL服务器实例,这是我之前做过的,并使用python和pymysql模块成功连接到它。唯一的区别是sql类型。
以前我打开的是MySQL服务器实例,而不是SQL Server实例。要连接到SQL Server,可以使用pyodbc;要连接到MySQL服务器,可以使用pymysql或mysql.connector。
无法通过python使用MySQL工具连接到SQL实例
即使使用命令行连接到MySQL实例,也可以使用sqlcmd命令,而要连接到Server,也可以使用mysql命令。
https://stackoverflow.com/questions/67630802
复制相似问题