正常的安装只须要执行如下2条命令:php
$ brew install mysql-connector-c $ pip3 install mysqlclient
但在执行 pip3 install mysqlclient
时,出现报错:python
which () { IFS="${IFS= }"; save_ifs="$IFS"; IFS=':' for file do :112 File "<string>", line 1, in <module> File "/private/var/folders/k9/q3cgwmcx4l51554_97h5rx4h0000gn/T/pip-install-n_ai97oc/mysqlclient/setup.py", line 16, in <module> metadata, options = get_config() File "/private/var/folders/k9/q3cgwmcx4l51554_97h5rx4h0000gn/T/pip-install-n_ai97oc/mysqlclient/setup_posix.py", line 63, in get_config libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')] File "/private/var/folders/k9/q3cgwmcx4l51554_97h5rx4h0000gn/T/pip-install-n_ai97oc/mysqlclient/setup_posix.py", line 63, in <listcomp> libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')] File "/private/var/folders/k9/q3cgwmcx4l51554_97h5rx4h0000gn/T/pip-install-n_ai97oc/mysqlclient/setup_posix.py", line 12, in dequote raise Exception("Wrong MySQL configuration: maybe https://bugs.mysql.com/bug.php?id=86971 ?") Exception: Wrong MySQL configuration: maybe https://bugs.mysql.com/bug.php?id=86971 ? ---------------------------------------- ERROR: Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/k9/q3cgwmcx4l51554_97h5rx4h0000gn/T/pip-install-n_ai97oc/mysqlclient/
缘由是,MySQL Connector/C使用了错误的默认配置,致使在Mac OS X下编译出错。mysql
所以,解决方法就是修改该默认配置。sql
执行命令 which mysql_config
以查看配置文件(mysql_config)的具体路径.数据库
$ which mysql_config /usr/local/bin/mysql_config # 输出了配置文件的真实路径
用你熟悉的编辑器打开该文件, 定位到112行。django
#原配置是: # Create options libs="-L$pkglibdir" libs="$libs -l " #修改为如下: # Create options libs="-L$pkglibdir" libs="$libs -lmysqlclient -lssl -lcrypto"
最后再执行前面的 $ pip3 install mysqlclient
, 已经能够正常安装了。编辑器
打开settings.py文件,修改 “DATABASES”设置code
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', #驱动名【保持默认】 'NAME': 'housework', #要链接的数据库名【改为你的数据库名】 'HOST': '127.0.0.1', #数据库地址【本地数据库保持默认就行】 'POST': 3306, #数据库端口【默认】 'USER': 'root', #你的数据库登陆名 'PASSWORD': 'admin12345', #你的数据库登陆密码 } }
到目前为止,Django就和数据库创建链接了。ip
附上官方文档: https://pypi.org/project/mysqlclient/ssl