适用于Python的MySQL连接器抛出“查询期间与MySQL服务器的连接丢失”

时间:2020-04-22 00:10:56

标签: python mysql mysql-connector-python

我当前正在使用Mac,几周前使用pip下载了MySQL连接器。使用我现在尝试运行的完全相同的文件,它可以正常工作。问题是,我现在收到此错误:

这是文件中的代码:

import mysql.connector

mindatabase=mysql.connector.connect(host='localhost', port=3306, user='Lagersjefen2020', passwd='lagerpassord', db='heltnydatabase')

settinn_markor=mindatabase.cursor()
markor=mindatabase.cursor()

settinn_markor.execute("INSERT INTO Vare"
                       "(VNr, Betegnelse, Pris, KatNr, Antall, Hylle)"
                       "VALUES('9999', 'Testvare', 99.99, 999, 99, 'T99')")

mindatabase.commit()

markor.execute('SELECT * FROM Vare')

for row in markor:
    print(row)

settinn_markor.close()
markor.close()

mindatabase.close()

这是我得到的错误:

Traceback (most recent call last):
  File "/Users/peterburkland/Desktop/tester1.py", line 8, in <module>
    settinn_markor.execute("INSERT INTO Vare"
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mysql/connector/cursor.py", line 569, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mysql/connector/connection.py", line 598, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mysql/connector/connection.py", line 354, in _send_cmd
    return self._socket.recv()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mysql/connector/network.py", line 267, in recv_plain
    raise errors.InterfaceError(errno=2013)
mysql.connector.errors.InterfaceError: 2013: Lost connection to MySQL server during query

我尝试运行其他文件也遇到类似的错误,但是在最后一行显示了“ mysql.connector.errors.ProgrammingError:1044(42000):用户被拒绝访问”。

1 个答案:

答案 0 :(得分:0)

仔细阅读错误消息:

GRANT ALL ON heltnydatabase.* TO Lagersjefen2020@localhost

然后您的用户可以修改/读取该数据库中的所有表。

其他更优的选择存在。只需执行一次。