Django: connect MySQL database in the OS X environment

Installation Library

Normal installation requires only the following two commands:

$ brew install mysql-connector-c
$ pip3 install mysqlclient

But in execution pip3 install mysqlclientwhen an error occurs:

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/

The reason is, MySQL Connector / C using the wrong default configuration, resulting in Mac OS X compiler error.

Therefore, the solution is to modify the default configuration.

Execute commands which mysql_configto view the configuration file (mysql_config) a specific path.

$ which mysql_config
/usr/local/bin/mysql_config   # 输出了配置文件的真实路径

Open the file in your favorite editor, navigate to the 112 line.

#原配置是:
# Create options
libs="-L$pkglibdir"
libs="$libs -l "

#修改成以下:
# Create options
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"

Finally, the implementation of the previous $ pip3 install mysqlclient, already properly installed.

Django configuration

Open the settings.py file, modify the "DATABASES" setting

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',  #驱动名【保持默认】
        'NAME': 'housework',                                     #要连接的数据库名【改成你的数据库名】
        'HOST': '127.0.0.1',                                     #数据库地址【本地数据库保持默认就行】
        'POST': 3306,                                                    #数据库端口【默认】
        'USER': 'root',                                              #你的数据库登录名
        'PASSWORD': 'admin12345',                            #你的数据库登录密码
    }
}

So far, Django and database to establish a connection.

Attach the official document: https://pypi.org/project/mysqlclient/

Guess you like

Origin www.cnblogs.com/ZJT7098/p/11331505.html