【问题标题】:(2059,“Authentication Plugin 'caching_sha2_password'”) when running server connected with MYSQL database on Django(2059,“Authentication Plugin 'caching_sha2_password'”) 在 Django 上运行连接 MYSQL 数据库的服务器时
【发布时间】:2019-07-05 03:34:05
【问题描述】:

我想配置我的 django 项目,以便将它与我使用 workbench 8.0 创建的 MYSQL 中的数据库连接,
然后我想通过运行来运行服务器

python manage.py runserver

从 anaconda 命令提示符,
这样我就可以使用 Django 界面来可视化和更改数据。
请注意,我不想降级 Workbench 8.0。

这些是我所做的步骤:

从 anaconda 提示符:

pip install mysqlclient

在我的项目文件夹中,在 settings.py 中

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'schema_meta',
        'USER': 'root',
        'PASSWORD': '<mypassword>',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    },
}

在mysql服务器的目录中,我打开cnf.ini并插入一个[client]部分:

[client]
database=schema_meta
host=127.0.0.1
user=root
password=<mypassword>
port=3306
default-character-set = utf8

然后我从 anaconda 提示符运行

Python manage.py runserver

我得到错误

django.db.utils.OperationalError: (2059, "身份验证插件 'caching_sha2_password' 无法加载:Impossibile trovare il 模规格。\r\n")

所以我尝试通过关注这个帖子来解决它:
django.db.utils.operationalError: (2059,"Authentication Plugin 'caching_sha2_password'")

我打开 mysql 工作台并运行以下查询:

delete from mysql.user
where user='root'
and host = '127.0.0.1';
flush privileges;
CREATE  USER 'root'@'127.0.0.1' IDENTIFIED WITH mysql_native_password BY '<mypassword>';

然后,我在 my.ini 文件中进行更改

default-authentication-plugin= caching_sha2_password

default-authentication-plugin=mysql_native_password

终于来自anaconda提示:

python manage.py runserver

但我又得到了

django.db.utils.OperationalError: (2059, "身份验证插件 'caching_sha2_password' 无法加载:Impossibile trovare il 模规格。\r\n")

现在,怎么了?为什么它没有对身份验证方法进行更改?

为了检查没有其他错误,从mysql工作台,从第一个“主页”视图,我右键单击我的数据库,打开“编辑连接”,单击“测试连接”,软件表示连接成功。

mysql workbench saying connection successfully established

此外,我想检查问题是否出在我的 Django 设置中。 所以我从 anaconda 提示符运行

pip install pymysql

然后在项目文件夹中,我创建了一个“connect_to_mysql.py”脚本,其中包含以下代码:

import pymysql.cursors
mydb = pymysql.connect(host='127.0.0.1',
                             user='root',
                             password='<mypassword>',
                             db='schema_meta',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)
print(mydb)

这似乎工作正常,因为当我运行时

connect_to_mysql.py 

从蟒蛇,我得到

pymysql.connections.Connection 对象位于 0x000002013F2851D0

我猜这意味着“连接成功建立”。
为了确保问题出在 mysql 中(我猜是 mysql 连接器),我创建了一个文件“connect_to_mysql_2.py”,其中包含以下代码:

import mysql.connector
mydb = mysql.connector.connect(user='root', password='<mypassword>',
                             host='127.0.0.1', database='meta_schema')
print(mydb)

当我从 anaconda 运行它时,我又得到了

"不支持身份验证插件'{0}'".format(plugin_name)) mysql.connector.errors.NotSupportedError:身份验证插件 不支持“caching_sha2_password”

这意味着我在 mysql 工作台和 my.ini 文件中没有解决任何问题。

如何让我的 Django 与我的 mysql 数据库连接并运行我的服务器?

有没有办法使用 pymysql 连接器而不是 mysql 连接器来建立服务器连接器?

【问题讨论】:

    标签: mysql django mysql-workbench mysql-connector pymysql


    【解决方案1】:

    这可能不是您的 python 代码中的问题,而是 python 连接器中的问题。 caching_sha2_password 插件现在是默认的身份验证插件,客户端必须支持它才能连接。因此,最好的解决方案是更新您的 python 连接器。另一种方法是禁用此插件,但我不建议这样做,因为它会降低服务器的安全性。

    【讨论】:

    • 我从dev.mysql.com/downloads/connector/python 安装了 mysql-connector-python-8.0.15-py3.7-windows-x86-64bit.msi 但是当我尝试运行服务器时,我又得到了同样的错误
    • 你应该改用pip install mysql-connector-python
    • 仍然没有任何变化:它说“要求已经满足:...”
    【解决方案2】:

    PyMySQL 在 0.9.0 中添加了对 caching_sha2_password 的支持,尽管在 0.9.1 中修复了 Py2 错误。

    同样在install instructions 中注明,对于caching_sha2_password 有额外的要求:

    python3 -m pip install PyMySQL[rsa]
    

    【讨论】:

      【解决方案3】:

      我想我解决了。

      关注本帖

      https://stackoverflow.com/a/21740692/7658051

      在 settings.py 的 DATABASES 条目中,我替换了

      'ENGINE': 'django.db.backends.mysql'
      

      'ENGINE': 'mysql.connector.django',
      

      再一次,如此处所述,

      https://django-mysql.readthedocs.io/en/latest/checks.html#django-mysql-w003-utf8mb4

      我也加了

      'OPTIONS': {
                  # Tell MySQLdb to connect with 'utf8mb4' character set
                  'charset': 'utf8mb4',
              },
              # Tell Django to build the test database with the 'utf8mb4' character set
              'TEST': {
                  'CHARSET': 'utf8mb4',
                  'COLLATION': 'utf8mb4_unicode_ci',
              }
      

      然后如果我跑了

      python manage.py runserver
      

      即使我无法访问 http://127.0.0.1:8000/admin ,它似乎也可以工作,因为我连接到旧数据库,所以我仍然需要检查 db 和我仍然需要学习的东西。

      当我运行时,作为连接现在正常工作的第二个证明

      connect_to_mysql_2.py
      

      (见(2059,“Authentication Plugin 'caching_sha2_password'”) when running server connected with MYSQL database on Django

      我终于明白了

      mysql.connector.connection_cext.CMySQLConnection 对象位于 0x000001DFB7521550

      所以我真的认为这次连接是打开的。

      【讨论】:

      • 我在尝试将 mysql 与 python 一起使用时有过可怕的经历。我建议改为使用 postgresql
      • 使用该引擎似乎会产生以下错误django.core.exceptions.ImproperlyConfigured: 'mysql.connector.django' isn't an available database backend. Try using 'django.db.backends.XXX', where XXX is one of: 'mysql', 'oracle', 'postgresql', 'sqlite3'
      猜你喜欢
      • 2018-11-01
      • 2018-10-05
      • 1970-01-01
      • 2017-02-06
      • 1970-01-01
      • 2013-07-02
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      相关资源
      最近更新 更多