[Django] Django 连接连接 MySQL数据库数据库 以及以及
makemigrations&migrate 过程详解总结过程详解总结
Django 连接连接 MySQL数据库过程数据库过程安装MySQL数据库安装mysqlclient包配置settings.py文件生成迁移文件ORM迁移文件具体操作生成数据库
的表
安装安装MySQL数据库数据库
Django自带的是SQLite数据库的, 如果要使用MySQL数据库, 则需要重新安装, 安装教程参考
Centos7安装MySQL8过程详解笔记 (附相关错误解决办法)
安装安装mysqlclient包包
python访问mysql数据库 需要第三方包, Django推荐使用mysqlclient.
安装命令pip3 install mysqlclient(blog) [root@centos7 testBlog]# yum install mysqlclient
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
No package mysqlclient available.
Error: Nothing to do
(blog) [root@centos7 testBlog]# pip3 install mysqlclient
Collecting mysqlclient
Using cached mysqlclient-1.4.6.tar.gz (85 kB)
ERROR: Command errored out with exit status 1:
command: /root/env/blog/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-yl6cmbzb/mysqlclient/setup.py'"'"'; __file__='"'"'/tmp/pip-install-
yl6cmbzb/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\n'"'"', '"'"''"'"');f.close();exec(compile(code, __file__,
'"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-yl6cmbzb/mysqlclient/pip-egg-info
cwd: /tmp/pip-install-yl6cmbzb/mysqlclient/
Complete output (12 lines):
/bin/sh: mysql_config: command not found
/bin/sh: mariadb_config: command not found
/bin/sh: mysql_config: command not found
Traceback (most recent call last):
File "", line 1, in
File "/tmp/pip-install-yl6cmbzb/mysqlclient/setup.py", line 16, in
metadata, options = get_config()
File "/tmp/pip-install-yl6cmbzb/mysqlclient/setup_posix.py", line 61, in get_config
libs = mysql_config("libs")
File "/tmp/pip-install-yl6cmbzb/mysqlclient/setup_posix.py", line 29, in mysql_config
raise EnvironmentError("%s not found" % (_mysql_config_path,))
OSError: mysql_config not found
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
上述报错显示mysql_config变量未找到, 需要安装mysql相关依赖
yum install mysql-devel
安装成功后, 继续执行pip3 install mysqlclient(blog) [root@centos7 bin]# pip3 install mysqlclient
Collecting mysqlclient
Using cached mysqlclient-1.4.6.tar.gz (85 kB)
Building wheels for collected packages: mysqlclient
Building wheel for mysqlclient (setup.py) ... error
ERROR: Command errored out with exit status 1:
command: /root/env/blog/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-rqkddtxm/mysqlclient/setup.py'"'"'; __file__='"'"'/tmp/pip-install-
rqkddtxm/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\n'"'"', '"'"''"'"');f.close();exec(compile(code, __file__,
'"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-eexcuz_5
cwd: /tmp/pip-install-rqkddtxm/mysqlclient/
Complete output (31 lines):
running bdist_wheel
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.6
creating build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/_exceptions.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/compat.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/connections.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/converters.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/cursors.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/release.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/times.py -> build/lib.linux-x86_64-3.6/MySQLdb
creating build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
running build_ext
building 'MySQLdb._mysql' extension
creating build/temp.linux-x86_64-3.6
creating build/temp.linux-x86_64-3.6/MySQLdb
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-
size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Dversion_info=(1,4,6,'final',0) -D__version__=1.4.6 -I/usr/include/mysql -