python - 无法在主机上将 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
解决方案
Pip install mysql-connector-python
然后导入mysql-connector。
推荐阅读
- reactjs - Express、React 的正文解析器问题。在数据库中发布 Null 值
- c - 如何编写与#ifdef...#else...#endif C 预处理器宏匹配的 awk 程序?
- .net - 使用 webrequest 的 API 返回失败
- javascript - 当我从菜单中单击链接标签时如何使导航栏消失
- reactjs - Webpack-dev-server 在第一次文件更改后停止更新
- html - 引导轮播控件不适用于 node.js
- ruby-on-rails - Heroku 备份的 Postgres 本地恢复将日期时间字段提前 4 小时
- python - pytest中的monkeypatching boto3 s3调用
- r - emmeans 输出上的 plot.CLD 可以进行哪些自定义?
- java - Android 应用因 Android 广告 ID 政策而不断被拒绝