mysql - 为什么可以通过 CLI 而不是通过应用程序连接到 MySQL 8?
问题描述
我不知何故无法将我的 python 应用程序连接到本地主机上的新 MySQL 服务器。pw 似乎没问题,因为我可以登录,但应用程序不能。
赠款:
mysql> show grants for test@localhost;
+-------------------------------------------------------------------------------+
| Grants for test@localhost |
+-------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `test`@`localhost` |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `Project`.* TO `test`@`localhost` |
+-------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
日志文件:
2021-02-11T10:49:13.897580Z 10 Connect crawler@localhost on Project using SSL/TLS
2021-02-11T10:49:13.897736Z 10 Connect Access denied for user 'test'@'localhost' (using password: YES)
但是,我可以从 CLI 连接:
$ mysql -u test -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.23 MySQL Community Server - GPL
日志文件:
2021-02-11T10:51:48.590555Z 11 Connect test@localhost on using Socket
2021-02-11T10:51:48.591527Z 11 Query select @@version_comment limit 1
密码包含此字符串:42$7&Z
并且长度为 16 个字符。
我已经设置:default_authentication_plugin= mysql_native_password
现在,但仍然没有变化。
来自 Python 3.8 的连接(适用于其他服务器):
def create_connection(self):
settings = get_project_settings()
self.conn = mysql.connector.connect(
host=settings.get('MYSQL_SERVER'),
user=settings.get('MYSQL_USER'),
passwd=settings.get('MYSQL_PW'),
database=settings.get('MYSQL_DB'),
charset='utf8'
)
self.conn._time_zone = '+00:00'
self.conn.autocommit = True
self.curr = self.conn.cursor()
self.curb = self.conn.cursor(buffered=True)
有什么我忽略的吗?
解决方案
尝试删除 MySQL'test'@'localhost'
用户并重新创建(说明如下)。然后授予权限,不要使用get_project_settings()
. 只需输入str
值。
要重新创建用户,请运行以下命令:
DROP USER 'test'@'localhost';
CREATE USER 'test'@'localhost' IDENTIFIED BY 'password'
这是带有str
值的修改后的代码(如果可行,请检查您的配置并使用您的代码重试):
def create_connection(self):
self.conn = mysql.connector.connect(
host='localhost',
user='test',
password='password,
database='database',
charset='utf8'
)
self.conn._time_zone = '+00:00'
self.conn.autocommit = True
self.curr = self.conn.cursor()
self.curb = self.conn.cursor(buffered=True)
推荐阅读
- mongodb - MongoDB聚合将列表转换为布尔值
- html - 在同一 div 中以固定的随机颜色悬停
- java - 如何在 cloud.stream.config.BindingServiceProperties 中硬编码绑定
- mysql - SQL 获取不属于类别的客户端
- focus - 使用对讲或画外音时,是否可以在模式弹出窗口中聚焦陷阱?
- css - “边框”不会呈现其容器的整个宽度
- python - 如何将常量值与 Python 中文本文件的每一行相乘?
- angular - 在Angular 2中正确等待来自Store的嵌套Observables
- node.js - 防止多次调用以刷新快速中间件中的会话
- flutter - 如何在 Flutter 中获取 Camera Intrinsic 参数