加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Django:在OS X环境下连接MySQL数据库

发布时间:2020-12-20 12:43:09 所属栏目:Python 来源:网络整理
导读:安装库 正常的安装只需要执行以下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

安装库

正常的安装只需要执行以下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",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就和数据库建立连接了。

附上官方文档: https://pypi.org/project/mysqlclient/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读