django 与 mysql 勾结指南

📖 阅读本文大概须要 26 分钟。php

参考文章:html

https://blog.51cto.com/eagle6899/2146972python

https://blog.csdn.net/qq_36963372/article/details/82558085mysql

第一步:配置 setting.py

# Database # https://docs.djangoproject.com/en/2.2/ref/settings/#databases DATABASES = { 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 'ENGINE': 'django.db.backends.mysql', 'NAME': 'mydatabase', 'USER': 'root', 'PASSWORD': 'root', 'HOST': '127.0.0.1', 'PORT': '3306', } } 

第二步:执行 migrate

$ python manage.py migrate 

不出意外会让你安装 mysqlclientsql

$ pip install mysqlclient 

你能下载成功,但可能安装失败。提示相似 “_mysql.c(29): fatal error C1083: 没法打开包括文件: “mysql.h”: No such file or directory” 的信息。数据库

总而言之,这是 window 开发者须要背负的穷罪。django

解决方案都在这里:https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclientbash

咱们的目标是手动选择一个适合的 mysqlclient.whl ,而后编译。网站


一、先安装 wheel,才能够编译 *.whl 文件

$ pip install wheel 

二、安装Microsoft Visual C++

Python 2.7:Microsoft Visual C++ 2008 (x64, x86, and SP1)ui

Python 3.x:Visual C++ 2017 (x64 or x86)

三、查看 pip 支持的版本

# AMD64 import pip._internal print(pip._internal.pep425tags.get_supported()) # WIN32 import pip print(pip.pep425tags.get_supported()) 

环境不一样,输出不一样,个人输入以下:

[('cp37', 'cp37m', 'win32'), ('cp37', 'none', 'win32'), ('py3', 'none', 'win32'), ('cp37', 'none', 'any'), ('cp3', 'none', 'any'), ('py37', 'none', 'any'), ('py3', 'none', 'any'), ('py36', 'none', 'any'), ('py35', 'none', 'any'), ('py34', 'none', 'any'), ('py33', 'none', 'any'), ('py32', 'none', 'any'), ('py31', 'none', 'any'), ('py30', 'none', 'any')] 

根据个人支持表,我找到了文件: mysqlclient-1.4.2-cp37-cp37m-win32.whl

你能够在这里查找:https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient

也能够在pip仓库查找各类历史版本:https://pypi.org/project/mysqlclient/#files

下载以后,进行安装

$ pip install mysqlclient-1.4.2-cp37-cp37m-win32.whl 

成功了你会看到以下输出:

Processing c:\users\lee\downloads\mysqlclient-1.4.2-cp37-cp37m-win32.whl Installing collected packages: mysqlclient Successfully installed mysqlclient-1.4.2 

若是是不正确的版本,你会出现以下报错:

mysqlclient-1.3.11-cp36-cp36m-win32.whl is not a supported wheel on this platform.

不须要担忧,慢慢找到匹配 pip 的便可。


一切尘埃落定以后,从新执行一下最初的 migratemigrate 命令。

$ python manage.py migrate 

若是你的 mysql 版本是 5.5(笔者用 phpstudy 最新版也只有5.5)。还会出现一个 SQL 错误的信息:

django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table ((1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(6) NOT NULL)' at line 1")) 

MySQL5.5并不支持Django2.1生成的这种SQL语句。我选择安装了 mysql lastest 版本。既 (mysql8.0.15)[https://dev.mysql.com/downloads/mysql/]

若是不会安装,请参考个人另外一篇建议笔记:mysql 编译安装 window篇

或者参考网站 mysql 安装教程。总之要确保运行中的 mysql 服务版本是 5.5 以上。

在确保你的 mysql 是最新且能访问以后。从新执行一下该命令。

python manage.py migrate 

若是成功会看到以下信息:

再看看你的数据库,django 生成了很多实用的表。

(完)

相关文章
相关标签/搜索