【问题标题】:Why is it possible to connect to MySQL 8 via CLI but not via an application?为什么可以通过 CLI 而不是通过应用程序连接到 MySQL 8?
【发布时间】:2021-02-11 10:56:22
【问题描述】:

不知何故,我的 python 应用程序无法连接到本地主机上的新 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)

有什么我忽略的吗?

【问题讨论】:

  • 我记得 mysql 8 与以前的版本相比,使用不同的方法来处理密码。查找有关如何更新 mysql server 8 的 pyton 代码的说明。或检查如何启用密码的兼容模式以使 mysql 使用旧方法
  • 对,我记得。现在将其设置为:default_authentication_plugin= mysql_native_password 但即使在重新启动服务器后它也是相同的
  • 请分享更多细节 - 您如何尝试在您的应用程序中连接?
  • 我在问题中添加了部分代码。这适用于其他系统。
  • 这能回答你的问题吗? Mysql localhost != 127.0.0.1?

标签: mysql


【解决方案1】:

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

【讨论】:

  • 我将用户的密码更改为不包含特殊字符并将其添加到 conf:default_authentication_plugin= mysql_native_password 这已解决问题,我相信您的解决方案与该方法类似。
猜你喜欢
  • 2020-12-25
  • 2018-12-04
  • 2017-11-24
  • 2014-04-13
  • 2019-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
相关资源
最近更新 更多