Django 连接 MySQL数据库过程
安装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('"'"'\r\n'"'"', '"'"'\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 "<string>", line 1, in <module> File "/tmp/pip-install-yl6cmbzb/mysqlclient/setup.py", line 16, in <module> 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.