django创建数据表报错 django.db.utils.OperationalError: (2059, <NULL>)

跟着网上的教程在pycharm中用mysqlclient连接数据库,创建了表,更改了setting配置

 

 

 

 

 接下来就应该使用命令同步数据库

python manage.py makemigrations

python manage.py migrate

输入第二个命令时报下面的错误, django服务也无法启动

D:\project\mysite>python manage.py runserver

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 244, in ensure_connection
self.connect()
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 225, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\mysql\base.py", line 244, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\MySQLdb\__init__.py", line 84, in Connect
return Connection(*args, **kwargs)
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\MySQLdb\connections.py", line 179, 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:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\commands\runserver.py", line 137, in inner_run
self.check_migrations()
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 576, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\loader.py", line 58, in __init__
self.build_graph()
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\loader.py", line 235, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\recorder.py", line 81, in applied_migrations
if self.has_table():
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\recorder.py", line 57, in has_table
with self.connection.cursor() as cursor:
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 284, in cursor
return self._cursor()
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 260, in _cursor
self.ensure_connection()
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 244, in ensure_connection
self.connect()
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 244, in ensure_connection
self.connect()
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 225, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\mysql\base.py", line 244, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\MySQLdb\__init__.py", line 84, in Connect
return Connection(*args, **kwargs)
File "C:\Users\xinzhi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\MySQLdb\connections.py", line 179, in __init__
super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2059, <NULL>)

 

查看了很多解决办法,尝试了一种后问题的到解决

问题所在:主要就是mysql8.0的问题。
目前最新的mysql8.0对用户密码的加密方式为caching_sha2_password, django暂时还不支持这种新增的加密方式。只需要将用户加密方式改为老的加密方式即可。

 

 

下面是cmd的操作命令:

mysql> use mysql;
Database changed

mysql> select user,plugin from user where user='root';
+------+-----------------------+
| user | plugin |
+------+-----------------------+
| root | caching_sha2_password |
+------+-----------------------+
1 row in set (0.00 sec)

mysql> alter user 'root'@'localhost' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

然后再在pycharm中执行python manage.py migrate命令问题得以解决

posted @ 2022-06-23 19:40  临夏1005  阅读(508)  评论(0编辑  收藏  举报