首先,确认数据库配置正确无误:
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:ProjectsPythonWebTestvenvlibsite-packagesdjangodbackendsasease.py", line 217, in ensure_connection self.connect() File "G:ProjectsPythonWebTestvenvlibsite-packagesdjangodbackendsasease.py", line 195, in connect self.connection = self.get_new_connection(conn_params) File "G:ProjectsPythonWebTestvenvlibsite-packagesdjangodbackendsmysqlase.py", line 227, in get_new_connection return Database.connect(**conn_params) File "C:Program Files (x86)PythonPython37-32libsite-packagesMySQLdb\__init__.py", line 84, in Connect return Connection(*args, **kwargs) File "C:Program Files (x86)PythonPython37-32libsite-packagesMySQLdbconnections.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)PythonPython37-32lib hreading.py", line 917, in _bootstrap_inner self.run() File "C:Program Files (x86)PythonPython37-32lib hreading.py", line 865, in run self._target(*self._args, **self._kwargs) File "G:ProjectsPythonWebTestvenvlibsite-packagesdjangoutilsautoreload.py", line 54, in wrapper fn(*args, **kwargs) File "G:ProjectsPythonWebTestvenvlibsite-packagesdjangocoremanagementcommands unserver.py", line 120, in inner_run self.check_migrations() File "G:ProjectsPythonWebTestvenvlibsite-packagesdjangocoremanagementase.py", line 453, in check_migrations executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS]) File "G:ProjectsPythonWebTestvenvlibsite-packagesdjangodbmigrationsexecutor.py", line 18, in __init__ self.loader = MigrationLoader(self.connection) File "G:ProjectsPythonWebTestvenvlibsite-packagesdjangodbmigrationsloader.py", line 49, in __init__ self.build_graph() File "G:ProjectsPythonWebTestvenvlibsite-packagesdjangodbmigrationsloader.py", line 212, in build_graph self.applied_migrations = recorder.applied_migrations() File "G:ProjectsPythonWebTestvenvlibsite-packagesdjangodbmigrations ecorder.py", line 73, in applied_migrations if self.has_table(): File "G:ProjectsPythonWebTestvenvlibsite-packagesdjangodbmigrations ecorder.py", line 56, in has_table return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()) File "G:ProjectsPythonWebTestvenvlibsite-packagesdjangodbackendsasease.py", line 256, in cursor return self._cursor() File "G:ProjectsPythonWebTestvenvlibsite-packagesdjangodbackendsasease.py", line 233, in _cursor self.ensure_connection() File "G:ProjectsPythonWebTestvenvlibsite-packagesdjangodbackendsasease.py", line 217, in ensure_connection self.connect() File "G:ProjectsPythonWebTestvenvlibsite-packagesdjangodbutils.py", line 89, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "G:ProjectsPythonWebTestvenvlibsite-packagesdjangodbackendsasease.py", line 217, in ensure_connection self.connect() File "G:ProjectsPythonWebTestvenvlibsite-packagesdjangodbackendsasease.py", line 195, in connect self.connection = self.get_new_connection(conn_params) File "G:ProjectsPythonWebTestvenvlibsite-packagesdjangodbackendsmysqlase.py", line 227, in get_new_connection return Database.connect(**conn_params) File "C:Program Files (x86)PythonPython37-32libsite-packagesMySQLdb\__init__.py", line 84, in Connect return Connection(*args, **kwargs) File "C:Program Files (x86)PythonPython37-32libsite-packagesMySQLdbconnections.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项目已经可以正常连接。