问题遇到的现象和发生背景
非IT从业者,最近因为业务的需要,自己搭建数据库。
MySQL部署电脑A上,现在想在电脑B上用Python连接MySQL,结果报错了,这就是本次要向大家请教的问题。
另外,在电脑B上通过ssh和workbe nch是能连接上电脑A的MySQL的。
问题相关代码,请勿粘贴截图
pycharm中使用的连接MySQL代码如下:
import pymysql.cursors
connect = pymysql.Connect(
host='XXXX',
port=3306,
user='XXX',
passwd='XXX',
db='test',
charset='utf8'
)
运行结果及报错内容
报错pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query') 。
详细报错信息见下:
Traceback (most recent call last):
File "/home/wangxl/SynologyDrive/Project/PycharmProjects/geo_data_extraction/project/fluidproject/000.py", line 9, in <module>
charset='utf8'
File "/home/wangxl/.conda/envs/py368/lib/python3.6/site-packages/pymysql/connections.py", line 353, in __init__
self.connect()
File "/home/wangxl/.conda/envs/py368/lib/python3.6/site-packages/pymysql/connections.py", line 632, in connect
self._get_server_information()
File "/home/wangxl/.conda/envs/py368/lib/python3.6/site-packages/pymysql/connections.py", line 1055, in _get_server_information
packet = self._read_packet()
File "/home/wangxl/.conda/envs/py368/lib/python3.6/site-packages/pymysql/connections.py", line 692, in _read_packet
packet_header = self._read_bytes(4)
File "/home/wangxl/.conda/envs/py368/lib/python3.6/site-packages/pymysql/connections.py", line 749, 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')
我的解答思路和尝试过的方法
通过https://dev.mysql.com/doc/refman/5.7/en/error-lost-connection.html?%ra=link,核查了MySQL的配置如下,应该是没有问题的。