环境
python==3.12.1
django==4.2.9
MySQL=5.7.44
报错信息
django.db.utils.NotSupportedError: MySQL 8 or later is required (found 5.7.44).
System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\IT\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner
self.run()
File "C:\IT\Python\Python312\Lib\threading.py", line 1010, in run
self._target(*self._args, **self._kwargs)
File "e:\lenovo_work\scheduleJob\envsche\Lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "e:\lenovo_work\scheduleJob\envsche\Lib\site-packages\django\core\management\commands\runserver.py", line 136, in inner_run
self.check_migrations()
File "e:\lenovo_work\scheduleJob\envsche\Lib\site-packages\django\core\management\base.py", line 574, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "e:\lenovo_work\scheduleJob\envsche\Lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "e:\lenovo_work\scheduleJob\envsche\Lib\site-packages\django\db\migrations\loader.py", line 58, in __init__
self.build_graph()
File "e:\lenovo_work\scheduleJob\envsche\Lib\site-packages\django\db\migrations\loader.py", line 235, in build_graph
self.applied_migrations = recorder.applied_migrations()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "e:\lenovo_work\scheduleJob\envsche\Lib\site-packages\django\db\migrations\recorder.py", line 81, in applied_migrations
if self.has_table():
^^^^^^^^^^^^^^^^
File "e:\lenovo_work\scheduleJob\envsche\Lib\site-packages\django\db\migrations\recorder.py", line 57, in has_table
with self.connection.cursor() as cursor:
^^^^^^^^^^^^^^^^^^^^^^^^
File "e:\lenovo_work\scheduleJob\envsche\Lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "e:\lenovo_work\scheduleJob\envsche\Lib\site-packages\django\db\backends\base\base.py", line 330, in cursor
return self._cursor()
^^^^^^^^^^^^^^
File "e:\lenovo_work\scheduleJob\envsche\Lib\site-packages\django\db\backends\base\base.py", line 306, in _cursor
self.ensure_connection()
File "e:\lenovo_work\scheduleJob\envsche\Lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "e:\lenovo_work\scheduleJob\envsche\Lib\site-packages\django\db\backends\base\base.py", line 289, in ensure_connection
self.connect()
File "e:\lenovo_work\scheduleJob\envsche\Lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "e:\lenovo_work\scheduleJob\envsche\Lib\site-packages\django\db\backends\base\base.py", line 272, in connect
self.init_connection_state()
File "e:\lenovo_work\scheduleJob\envsche\Lib\site-packages\django\db\backends\mysql\base.py", line 257, in init_connection_state
super().init_connection_state()
File "e:\lenovo_work\scheduleJob\envsche\Lib\site-packages\django\db\backends\base\base.py", line 239, in init_connection_state
self.check_database_version_supported()
File "e:\lenovo_work\scheduleJob\envsche\Lib\site-packages\django\db\backends\base\base.py", line 214, in check_database_version_supported
raise NotSupportedError(
django.db.utils.NotSupportedError: MySQL 8 or later is required (found 5.7.44).
解决方案
1、升级MySQL到8以上(略)
2、重写方法(优雅)
在settings.py同级目录下配置hexception.py文件(文件可以灵活设置),代码如下:
def check_database_version_supported(self):
"""
在这里修改 check_database_version_supported 方法以适配 MySQL 5.7
"""
if (
self.features.minimum_database_version is not None
and self.get_database_version() < self.features.minimum_database_version
):
db_version = ".".join(map(str, self.get_database_version()))
min_db_version = ".".join(map(str, self.features.minimum_database_version))
print(f"WARNNING : The current MySQL version: {db_version} The recommended MySQL version:{min_db_version}")
settings.py中引用:
from .hexception import check_database_version_supported
from django.db.backends.base.base import BaseDatabaseWrapper
BaseDatabaseWrapper.check_database_version_supported = check_database_version_supported
原理
...\django\db\backends\base\base.py", line 214, in check_database_version_supported
这一块说明了抛出错误的出处,所以重写此处的方法能将影响降到最低。
我将我写的方法替换django原模块的方法,从而解决该问题