【发布时间】:2022-01-18 21:01:50
【问题描述】:
当我尝试在 Google Cloud Platform 上使用 Python 连接到 MySql 数据库服务器时,我收到了错误消息。以下是实际代码。
import mysql.connector
import pandas as pd
cnx = mysql.connector.connect(user = 'xxxxxx', password='xxxxxx',
host='xxx.xxx.xxx.xx',
database='xxxxxxxxx')
cursor = cnx.cursor()
cnx.close()
if cur and con:
cur.close()
con.close()
sql1 = "SELECT * FROM ms_trackevaluation_15_16.ms_skill"
cursor.execute(sql1)
rows = cursor.fetchall()
df1 = pd.read_sql(sql1, cnx)
以下是错误:
InterfaceError Traceback (most recent call last)
<ipython-input-2-30094be976fb> in <module>()
4 cnx = mysql.connector.connect(user = 'xxxxx', password='xxxxx',
5 host='173.194.104.33',
----> 6 database='xxxxx')
7 cursor = cnx.cursor()
8 cnx.close()
/home/scrollstech_shravankumar/anaconda3/lib/python3.5/site-packages/mysql/connector/__init__.py in connect(*args, **kwargs)
177 return CMySQLConnection(*args, **kwargs)
178 else:
--> 179 return MySQLConnection(*args, **kwargs)
180 Connect = connect # pylint: disable=C0103
181
/home/scrollstech_shravankumar/anaconda3/lib/python3.5/site-packages/mysql/connector/connection.py in __init__(self, *args, **kwargs)
93
94 if len(kwargs) > 0:
---> 95 self.connect(**kwargs)
96
97 def _do_handshake(self):
/home/scrollstech_shravankumar/anaconda3/lib/python3.5/site-packages/mysql/connector/abstracts.py in connect(self, **kwargs)
714
715 self.disconnect()
--> 716 self._open_connection()
717 self._post_connection()
718
/home/scrollstech_shravankumar/anaconda3/lib/python3.5/site-packages/mysql/connector/connection.py in _open_connection(self)
205 self._socket = self._get_connection()
206 self._socket.open_connection()
--> 207 self._do_handshake()
208 self._do_auth(self._user, self._password,
209 self._database, self._client_flags, self._charset_id,
/home/scrollstech_shravankumar/anaconda3/lib/python3.5/site-packages/mysql/connector/connection.py in _do_handshake(self)
97 def _do_handshake(self):
98 """Get the handshake from the MySQL server"""
---> 99 packet = self._socket.recv()
100 if packet[4] == 255:
101 raise errors.get_exception(packet)
/home/scrollstech_shravankumar/anaconda3/lib/python3.5/site-packages/mysql/connector/network.py in recv_plain(self)
241 chunk = self.sock.recv(4 - packet_len)
242 if not chunk:
--> 243 raise errors.InterfaceError(errno=2013)
244 packet += chunk
245 packet_len = len(packet)
InterfaceError: 2013: Lost connection to MySQL server during query
谁能解释这段代码有什么问题以及为什么我会收到这个错误。请解释我该如何解决这个问题?
【问题讨论】:
-
你能检查一下你的mysql服务器是否暴露了端口用于通信。默认情况下它的 3306 除非你已经修改它。似乎只是网络问题。
-
我没有修改过。端口号与根服务器和我现在使用的服务器相同。
-
然后在你的服务器中寻找防火墙,也许那是限制默认端口上的通信。
标签: mysql python-3.x pandas