(2059,“身份验证插件'caching_sha2_password'”)在Django上运行与MYSQL数据库连接的服务器时

时间:2019-02-11 15:33:07

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

我想配置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”:可能的错误   模数比。\ 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”:可能的错误   模数比。\ 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连接器来建立服务器连接器?

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我想我解决了。

通过此线程

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,它也似乎可以正常工作,因为我已连接到旧数据库,因此我仍然必须检查数据库和仍需学习的内容。

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

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

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

答案 2 :(得分:0)

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

install instructions中也指出,对于caching_sha2_password,还有其他要求:

python3 -m pip install PyMySQL[rsa]