45
Django:在OS X环境下连接MySQL数据库
source link: https://www.tuicool.com/articles/2ENzI3U
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
安装库
正常的安装只需要执行以下2条命令:
$ brew install mysql-connector-c $ pip3 install mysqlclient
但在执行 pip3 install mysqlclient
时,出现报错:
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下编译出错。
因此,解决方法就是修改该默认配置。
执行命令 which mysql_config
以查看配置文件(mysql_config)的具体路径.
$ which mysql_config /usr/local/bin/mysql_config # 输出了配置文件的真实路径
用你熟悉的编辑器打开该文件, 定位到 112 行。
#原配置是: # Create options libs="-L$pkglibdir" libs="$libs -l " #修改成以下: # Create options libs="-L$pkglibdir" libs="$libs -lmysqlclient -lssl -lcrypto"
最后再执行前面的 $ pip3 install mysqlclient
, 已经可以正常安装了。
Django配置
打开settings.py文件,修改 “DATABASES”设置
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', #驱动名【保持默认】 'NAME': 'housework', #要连接的数据库名【改成你的数据库名】 'HOST': '127.0.0.1', #数据库地址【本地数据库保持默认就行】 'POST': 3306, #数据库端口【默认】 'USER': 'root', #你的数据库登录名 'PASSWORD': 'admin12345', #你的数据库登录密码 } }
到目前为止,Django就和数据库建立连接了。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK