0%

关于Django中连接MySQL报错的解决方法

关于Django中连接mysql数据库报错的解决方法:RuntimeError: 'cryptography' package is required for sha256_password or caching_sha2_password auth methods

一、问题发现

安装MySQL8的条件下,在Django中连接MySQL数据库时会发生如下错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\site-packages\django\core\management\commands\runserver.py", line 120, in inner_run
self.check_migrations()
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\site-packages\django\core\management\base.py", line 453, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__
self.build_graph()
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\site-packages\django\db\migrations\loader.py", line 212, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\site-packages\django\db\migrations\recorder.py", line 73, in applied_migrations
if self.has_table():
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\site-packages\django\db\migrations\recorder.py", line 56, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\site-packages\django\db\backends\base\base.py", line 256, in cursor
return self._cursor()
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\site-packages\django\db\backends\base\base.py", line 233, in _cursor
self.ensure_connection()
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection
self.connect()
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\site-packages\django\db\backends\base\base.py", line 195, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\site-packages\django\db\backends\mysql\base.py", line 227, in get_new_connection
return Database.connect(**conn_params)
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\site-packages\pymysql\__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\site-packages\pymysql\connections.py", line 327, in __init__
self.connect()
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\site-packages\pymysql\connections.py", line 588, in connect
self._request_authentication()
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\site-packages\pymysql\connections.py", line 874, in _request_authentication
auth_packet = _auth.caching_sha2_password_auth(self, auth_packet)
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\site-packages\pymysql\_auth.py", line 326, in caching_sha2_password_auth
data = sha2_rsa_encrypt(conn.password, conn.salt, conn.server_public_key)
File "C:\Users\zqchen7\Anaconda3\envs\dev_2.2_3.6\lib\site-packages\pymysql\_auth.py", line 204, in sha2_rsa_encrypt
raise RuntimeError("'cryptography' package is required for sha256_password or caching_sha2_password auth methods")
RuntimeError: 'cryptography' package is required for sha256_password or caching_sha2_password auth methods

二、问题分析

原因是从MySQL8开始后使用新的加密方式 caching-sha2-password,而之前使用的加密方式为mysql_native_password,因此hi存在不兼容的情况。

三、问题解决

汇总网上现有的方法共计三种解决方案(三选一即可): + 把root用户的密码加密方式改为mysql_native_password

  1. windows下安装的MySQL8客户端,打开MySQL 8.0 Command Line Client
在这里插入图片描述
  1. 输入ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '1234';更新一下用户的密码为1234,密码可以按照自己的需求进行更改。

  2. 刷新权限FLUSH PRIVILEGES; #刷新权限

在这里插入图片描述
  • 安装'cryptography'(最简单,推荐使用)

    有虚拟环境的先进虚拟环境conda activate ***,没有虚拟环境的直接在命令行窗口中敲如下代码

    pip install cryptography

  • 使用DBeaver的用户在设置中的驱动属性allowPublicKeyRetrieval改为True(每次重启电脑后都需要重新设置一次,不推荐)

  1. 右击localhost点击编辑连接
在这里插入图片描述
  1. 修改驱动属性allowPublicKeyRetrieval改为True 在这里插入图片描述

  2. 重新连接,点击是

在这里插入图片描述