代码之家  ›  专栏  ›  技术社区  ›  Tms91

(2059,身份验证插件'caching sha2_password')在Django上运行与MySQL数据库连接的服务器时

  •  0
  • Tms91  · 技术社区  · 7 年前

    我想配置我的Django项目,以便将其与中的数据库连接。 MySQL 我创造了 工作台8 ,
    然后我想通过运行

    python manage.py runserver
    

    在“水蟒”命令提示下,
    这样我就可以使用django接口来可视化和更改数据。
    请注意,我不想降级Workbench 8.0。

    以下是我所做的步骤:

    来自蟒蛇提示:

    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 server目录下,我打开cnf.ini,插入一个[客户端]部分:

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

    然后从蟒蛇的提示下我跑了

    Python manage.py runserver
    

    我得到了错误

    django.db.utils.operationalerror:(2059,“身份验证插件” 无法加载“缓存密码”:不可能的trovare il modulo specificato.\r\n“)

    所以我试着按照这个思路来解决它:
    django.db.utils.operationalError: (2059,"Authentication Plugin 'caching_sha2_password'")

    我打开MySQL Workbench并运行此查询:

    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.in i文件中,我更改

    default-authentication-plugin= caching_sha2_password
    

    default-authentication-plugin=mysql_native_password
    

    最后从蟒蛇提示:

    python manage.py运行服务器
    

    但我再一次得到

    django.db.utils.operationalerror:(2059,“身份验证插件” 无法加载“缓存密码”:不可能的trovare il modulo specificato.\r\n“)

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

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

    mysql workbench saying connection successfully established

    此外,我想检查一下问题是否在我的Django设置中。 所以在水蟒的提示下我跑了

    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 
    

    我从水蟒那里得到

    位于0x000002013f2851d0的pymysql.connections.connection对象

    我想这意味着成功建立了联系。
    为了确保mysql(我猜是mysql connector)出现问题,我创建了一个文件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)
    

    当我从水蟒身上跑出来的时候

    “不支持身份验证插件”0“。格式(插件名称)) mysql.connector.errors.notSupportedError:身份验证插件 不支持“缓存\u sha2 \u密码”

    这意味着我在mysql工作台和我的.in i文件中没有修改任何内容。

    如何让我的django连接到我的mysql数据库和运行我的服务器?

    是否有方法使用pymysql connector而不是mysql connector建立服务器连接器?

    3 回复  |  直到 7 年前
        1
  •  0
  •   Mike Lischke    7 年前

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

        2
  •  0
  •   Tms91    7 年前

    我想我解决了。

    通过跟踪这个线索

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

    在settings.py中,在数据库条目处,我用

    '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运行服务器

    它似乎有效,即使我不能进入 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

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

        3
  •  0
  •   danblack    7 年前

    PyMySQL 为添加了支持 caching_sha2_password 在0.9.0中,尽管在0.9.1中修复了PY2错误。

    也在 install instructions ,为了 缓存\ sha2 \密码 还有其他要求:

    python3 -m pip install PyMySQL[rsa]
    
    推荐文章