首页 > 解决方案 > 为什么可以通过 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

解决方案


尝试删除 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)

推荐阅读