Django3.0+Python3.8+MySQL8.0 个人博客搭建五|makemigrations连接MySQL数据库的坑

一、执行makemigrations命令

在让Django与MySQL关联后,要让Python也和MySQL连接上,需要在虚拟环境中执行makemigrations命令

(fswy) xxxxdeMacBook-Pro:blog xiatian$ python3 manage.py makemigrations

二、报错

后面报了一系列的错误,在此做个总结

错误1:Error loading MySQLdb module.
Traceback (most recent call last):
  File "/Users/xiatian/fswy/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 16, in <module>
    import MySQLdb as Database
ModuleNotFoundError: No module named 'MySQLdb'
......
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
原因:

Python2中用的是MySQLdb,但在Python3中用的是 mysqlclient

解决方案:

安装mysqlclient

(fswy) xxxxdeMacBook-Pro:blog xiatian$ pip3 install mysqlclient
Collecting mysqlclient
Successfully installed mysqlclient-1.4.6
重现输入:$ python3 manage.py makemigrations
错误2:Reason: image not found
  File "/Users/xiatian/fswy/lib/python3.8/site-packages/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: dlopen(/Users/xiatian/fswy/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib
  Referenced from: /Users/xiatian/fswy/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so
    Reason: image not found
原因:未在_ _ init_ _.py文件伪装MySQL数据库

这里死的很惨,试过各种方法
安装各种模块:

$ pip3 install mysql-connector-python-rf
$ pip3 install mysql-python
$ pip3 install MySQLdb

不过,好像都是python2.x的解决方案,这里都不支持python3.8

解决方案:伪装mysql数据库

来到App文件夹下面的 _ _ init_ _.py文件,在里面直接添加如下代码:

import pymysql

pymysql.install_as_MySQLdb()

效果如下:
在这里插入图片描述

重现输入:$ python3 manage.py makemigrations
错误3:mysqlclient 1.3.13 or newer is required
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
原因:

由于上面安装的pymysql版本低,但是python3又不支持高版本,所以只能靠改变设置文件来更改配置了

解决方案:

进入/Users/xiatian/fswy/blog/venv/lib/python3.8/site-packages/django/db/backends/mysql/base.py中将其第36、37行注释掉
在这里插入图片描述

重现输入:$ python3 manage.py makemigrations

发现还是报错同样的错误

解决方案:

打开访达,前往:/Users/xiatian/fswy/blog/venv/lib/python3.8/site-packages/django/db/backends/mysql/base.py,发现base.py中并没有被注释掉第36、37行,这里用文本编辑打开,再次注释掉第36、37行。
在这里插入图片描述

重现输入:$ python3 manage.py makemigrations
错误4: Warning: 3135
/Users/xiatian/fswy/lib/python3.8/site-packages/pymysql/cursors.py:170: Warning: (3135, "'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.")
原因:

中文意思是:警告:(3135,"‘NO_ZERO_DATE’, ‘NO_ZERO_IN_DATE’和’ error_for_division on_by_zero ’ sql模式应该与严格模式一起使用。它们将在未来的版本中与strict模式合并。”

就是说SQL modes应该使用安全模式,即该功能将可能在未来的版本中被舍弃或者合并,这是MySQL向后兼容常见的处理方式

解决方案:

把setting里的options注释掉就正常了

在这里插入图片描述

重现输入:$ python3 manage.py makemigrations
错误5:No changes detected
No changes detected
原因:

有时执行python manage.py makemigrations命令(也可能人比较皮,把migrations文件夹给删了),会提示"No changes detected."

解决方案:

python3 manage.py makemigrations --empty yourappname生成一个空的initial.py

(fswy) xxxxdeMacBook-Pro:blog xiatian$ python3 manage.py makemigrations --empty fswy
Migrations for 'fswy':
  fswy/migrations/0001_initial.py

python3 manage.py migrate 创建数据库表单

(fswy) xxxxdeMacBook-Pro:blog xiatian$ python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, firstWEB, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying firstWEB.0001_initial... OK
  Applying sessions.0001_initial... OK

在这里插入图片描述

至此,终于将Python3.8和MySQL8.0连接。

希望此文能给予大家帮助!

参考网址:
Mac下python3安装mysqlclient

python3 manage.py makemigrations执行可能出现的错误

django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.2

使用python建立一个网站:笔记5 在Django3.0中配置Mysql8 版本(win10环境)

django使用mysql出现警告Warning: (3135, "‘NO_ZERO_DATE’, ‘NO_ZERO_IN_DATE’ and ‘ERROR_FOR_DIVISION_BY_ZERO’…

python manage.py makemigrations No changes detected

原创文章 214 获赞 359 访问量 89万+

猜你喜欢

转载自blog.csdn.net/yxys01/article/details/105707711
今日推荐