zoukankan      html  css  js  c++  java
  • Django:在OS X环境下连接MySQL数据库

    安装库

    正常的安装只需要执行以下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',									 #数据库地址【本地数据库保持默认就行】
            'PORT': 3306,													 #数据库端口【默认】
            'USER': 'root',												 #你的数据库登录名
            'PASSWORD': 'admin12345',							 #你的数据库登录密码
        }
    }
    

    到目前为止,Django就和数据库建立连接了。

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

  • 相关阅读:
    系统边界确定
    用例分析技术:确定系统边界
    系统边界的定义
    【转】读《程序员修炼之道》
    项目管理之需求基线管理
    字符串流sstream[part3/使用字符串流进行安全的类型转换]
    字符串流sstream[part2/使用同一个字符串流反复读写数据]
    字符串流sstream[part1/基本知识]
    文件操作的openmode
    文件操作的一些函数
  • 原文地址:https://www.cnblogs.com/ZJT7098/p/11331505.html
Copyright © 2011-2022 走看看