首页 > 解决方案 > 无法在主机上将 Python 连接到 MySQL。错误 1227 (42000)、错误 1227 (42000)

问题描述

我使用 python 和 MySQL 制作了一个电报机器人。我可以在终端中输入数据库,但是当我运行我的 python 代码时,它说我没有权限。

      ilyacherne@vh289 ~ $ python3 /home/i/ilyacherne/public_html/cgi-bin/bot.py
Traceback (most recent call last):
  File "/home/i/ilyacherne/.local/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 239, in _open_connection
    self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: Access denied for user 'ilyacherne'@'77.222.61.25' (using password: YES)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/i/ilyacherne/public_html/cgi-bin/bot.py", line 3, in <module>
    import config
  File "/home/i/ilyacherne/public_html/cgi-bin/config.py", line 8, in <module>
    database = mysql.connect(
  File "/home/i/ilyacherne/.local/lib/python3.8/site-packages/mysql/connector/__init__.py", line 272, in connect
    return CMySQLConnection(*args, **kwargs)
  File "/home/i/ilyacherne/.local/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 85, in __init__
    self.connect(**kwargs)
  File "/home/i/ilyacherne/.local/lib/python3.8/site-packages/mysql/connector/abstracts.py", line 1009, in connect
    self._open_connection()
  File "/home/i/ilyacherne/.local/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 241, in _open_connection
    raise errors.get_mysql_exception(msg=exc.msg, errno=exc.errno,
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'ilyacherne'@'77.222.61.25' (using password: YES)

我检查了密码、主机名、数据库名和用户。我用这段代码连接:

database = mysql.connect(
    host = '77.222.61.25',
    user = 'ilyacherne',
    passwd = 'CENSORED',
    database = 'ilyacherne'
)

支持人员说 MySql 5.7 默认连接到套接字,我只使用“sudo mysql”连接,然后他们建议我试试这个:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Current-Root-Password'; FLUSH PRIVILEGES;

但它说:

ERROR 1227 (42000): Access denied; you need (at least one of) the CREATE USER privilege(s) for this operation
ERROR 1227 (42000): Access denied; you need (at least one of) the RELOAD privilege(s) for this operation

标签: pythonmysql

解决方案


  Pip install mysql-connector-python

然后导入mysql-connector。


推荐阅读