首先,确认数据库配置正确无误:
DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, # or use: mysql.connector.django ‘NAME‘: ‘TestDB‘, ‘USER‘: ‘Guest‘, ‘PASSWORD‘: ‘admin‘, ‘HOST‘: ‘192.168.218.199‘, ‘PORT‘: ‘3306‘, } }
但是启动项目时仍然出现如下错误:
System check identified no issues (0 silenced). Exception in thread django-main-thread: Traceback (most recent call last): File "G:\Projects\Python\WebTest\venv\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection self.connect() File "G:\Projects\Python\WebTest\venv\lib\site-packages\django\db\backends\base\base.py", line 195, in connect self.connection = self.get_new_connection(conn_params) File "G:\Projects\Python\WebTest\venv\lib\site-packages\django\db\backends\mysql\base.py", line 227, in get_new_connection return Database.connect(**conn_params) File "C:\Program Files (x86)\Python\Python37-32\lib\site-packages\MySQLdb\__init__.py", line 84, in Connect return Connection(*args, **kwargs) File "C:\Program Files (x86)\Python\Python37-32\lib\site-packages\MySQLdb\connections.py", line 164, in __init__ super(Connection, self).__init__(*args, **kwargs2) MySQLdb._exceptions.OperationalError: (2059, <NULL>) The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Program Files (x86)\Python\Python37-32\lib\threading.py", line 917, in _bootstrap_inner self.run() File "C:\Program Files (x86)\Python\Python37-32\lib\threading.py", line 865, in run self._target(*self._args, **self._kwargs) File "G:\Projects\Python\WebTest\venv\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper fn(*args, **kwargs) File "G:\Projects\Python\WebTest\venv\lib\site-packages\django\core\management\commands\runserver.py", line 120, in inner_run self.check_migrations() File "G:\Projects\Python\WebTest\venv\lib\site-packages\django\core\management\base.py", line 453, in check_migrations executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS]) File "G:\Projects\Python\WebTest\venv\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__ self.loader = MigrationLoader(self.connection) File "G:\Projects\Python\WebTest\venv\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__ self.build_graph() File "G:\Projects\Python\WebTest\venv\lib\site-packages\django\db\migrations\loader.py", line 212, in build_graph self.applied_migrations = recorder.applied_migrations() File "G:\Projects\Python\WebTest\venv\lib\site-packages\django\db\migrations\recorder.py", line 73, in applied_migrations if self.has_table(): File "G:\Projects\Python\WebTest\venv\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 "G:\Projects\Python\WebTest\venv\lib\site-packages\django\db\backends\base\base.py", line 256, in cursor return self._cursor() File "G:\Projects\Python\WebTest\venv\lib\site-packages\django\db\backends\base\base.py", line 233, in _cursor self.ensure_connection() File "G:\Projects\Python\WebTest\venv\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection self.connect() File "G:\Projects\Python\WebTest\venv\lib\site-packages\django\db\utils.py", line 89, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "G:\Projects\Python\WebTest\venv\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection self.connect() File "G:\Projects\Python\WebTest\venv\lib\site-packages\django\db\backends\base\base.py", line 195, in connect self.connection = self.get_new_connection(conn_params) File "G:\Projects\Python\WebTest\venv\lib\site-packages\django\db\backends\mysql\base.py", line 227, in get_new_connection return Database.connect(**conn_params) File "C:\Program Files (x86)\Python\Python37-32\lib\site-packages\MySQLdb\__init__.py", line 84, in Connect return Connection(*args, **kwargs) File "C:\Program Files (x86)\Python\Python37-32\lib\site-packages\MySQLdb\connections.py", line 164, in __init__ super(Connection, self).__init__(*args, **kwargs2) django.db.utils.OperationalError: (2059, <NULL>)
原因:Django 对Mysql 8.0 版本的默认密码加密方式不支持
解决方法:修改密码加密方式。
首先做查询操作看一下plugin:
use mysql; select user,plugin from user;
结果如下:
Mysql 官方原文:
In MySQL 8.0, caching_sha2_password
is the default authentication plugin rather than mysql_native_password
.
链接:https://dev.mysql.com/doc/refman/8.0/en/sha256-pluggable-authentication.html
但是Django对caching_sha2_password不支持
所以我们下一步就是对其进行修改:
alter user ‘Guest‘@‘%‘ identified with mysql_native_password by ‘admin‘;
此时已经修改成功,我们再次查询看一下结果:
重新开启Django项目已经可以正常连接。
原文地址:https://www.cnblogs.com/zoneofmine/p/10801192.html
时间: 2024-10-08 18:53:29