首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用pymysql连接到cloudcluster mysql时出现问题

使用pymysql连接到cloudcluster mysql时出现问题
EN

Stack Overflow用户
提问于 2021-05-21 12:25:14
回答 1查看 37关注 0票数 0

我使用以下代码连接到我的云主机(注X是实际凭证的占位符)。是的,我已经环顾四周,我没有找到有用的答案。

代码语言:javascript
复制
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)}')

我得到了下面的错误

代码语言:javascript
复制
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

在这件事上的任何帮助都将受到高度的感谢。

EN

回答 1

Stack Overflow用户

发布于 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命令。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67630802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档