mac + python3 + django2.2 + mysql 遇到数据库连接包问题

Django用到了2.2版本, 开始搭建了新的服务, 出师未捷先采坑

到达数据库迁移的时候, 出现了错误

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

这是一个很烦心的事情, 于是去搜索了一下

大致就是说,安装pymysql包

pip install pymysql

然后在与想settings.py 同一文件夹的_init_.py中增加

import pymysql

pymysql.install_as_MySQLdb()

增加完成了之后又出现了错误

File "/Users/lixiang/.env/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 36, in <module>
    raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

说你的pymysql的版本是0.9.3, 同时需要mysqlclient需要1.3.13(记住这句话, 标记为 ”错误理解“),我有傻傻的去安装mysqlclient,

pip install mysqlclient==1.3.13

结果还是原来的错误, 还是原来的懵逼

继续查, 有查到了解决方案

方案是去Django的包中改代码

路径是: /Users/lixiang/.env/lib/python3.6/site-packages/django/db/backends/mysql/base.py

把这行

if version < (1, 3, 13):
   raise ImproperlyConfigured(
       'mysqlclient 1.3.13 or newer is required; you have %s.'
       % Database.__version__
   )
   

改成这样, 也就是注释:

if version < (1, 3, 13):
   pass
   '''
   raise ImproperlyConfigured(
       'mysqlclient 1.3.13 or newer is required; you have %s.'
       % Database.__version__
   )
   '''

然后在改另一个文件

路径是: /Users/lixiang/.env/lib/python3.6/site-packages/django/db/backends/mysql/operations.py

把这行

query = query.decode(errors='replace')

改成

query = query.encode(errors='replace')

完美解决

(.env) ➜  cws python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK

但是确实有点瑕疵

就是因为我在修改/Users/lixiang/.env/lib/python3.6/site-packages/django/db/backends/mysql/base.py文件是时候发现了这样一块

MySQL database backend for Django.

Requires mysqlclient: https://pypi.org/project/mysqlclient/

显示django mysql数据后台, 需要mysqlclient包, 地址为


忽然有些明白了, Django后台需要的包是不是mysqlclient, 查查看

mysqlclient是python3的mysql版本, 这么一想pymysql我做的这些东西都是为了兼容, 上边理解错误的话, 其实是把pymsql的0.9.3版本 当成了mysqlclient版本

还是使用mysqlclient吧

上边mysqlclient的地址已经有很详细的安装说明了, 主要说一下mac上的安装, 需要先安装mysql-connector-c

brew install mysql-connector-c # macOS (Homebrew) (Currently, it has bug. See below)

因为我已经装过了, 这里重装一下,为了掩饰(安装: brew install mysql-connector-c)

(.env) ➜  brew reinstall mysql-connector-c
==> Reinstalling mysql-connector-c
==> Downloading https://homebrew.bintray.com/bottles/mysql-connector-c-6.1.11.high_sierra.bottle.tar.
Already downloaded: /Users/lixiang/Library/Caches/Homebrew/downloads/c68782c842162dcb79a96dd6b880e906521be7595069066f870426f1de9aba35--mysql-connector-c-6.1.11.high_sierra.bottle.tar.gz
==> Pouring mysql-connector-c-6.1.11.high_sierra.bottle.tar.gz
   /usr/local/Cellar/mysql-connector-c/6.1.11: 79 files, 15.3MB

然后修改mysql_config

vim /usr/local/Cellar/mysql-connector-c/6.1.11/bin/mysql_config

因为有bug, 需要将

# on macOS, on or about line 112:
# Create options
libs="-L$pkglibdir"
libs="$libs -l "

改为

# Create options
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"

这个ssl也可能有问题, 下面链接可以解决

OSX ld: library not found for -lssl

然后做一次link

(.env) ➜  brew link mysql-connector-c
Linking /usr/local/Cellar/mysql-connector-c/6.1.11... 48 symlinks created

最后强制mysqlclient重新编译和安装:

pip install --force-reinstall --ignore-installed --no-binary :all: mysqlclient

我本地安装mysql客户端, 使用的是docker, 可能你还有问题, 可以使用下面链接解决

发布于 2019-08-06 17:48