为什么可以通过 CLI 而不是通过应用程序连接到 MySQL 8?

时间:2021-02-11 10:56:22

标签: mysql

我以某种方式无法将我的 pyhton 应用程序连接到本地主机上的新 MySQL 服务器。密码好像可以,我可以登录,但应用程序不能。

赠款:

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)

有什么我忽略的地方吗?

1 个答案:

答案 0 :(得分:0)

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