首先说一下,mysqlclient现在支持python3,mysqlclient具体哪个版本支持的不太清楚。想的自己去看下文档说明
This project adds Python 3 support and bug fixes. I hope this fork is merged back to MySQLdb1 like distribute was merged back to setuptools.
原来Django一直使用的Mondb数据库,由于项目需要使用mysql,配置mysql时一直失败,报错提示
django.core.exceptions.ImproperlyConfigured:
mysqlclient 1.3.13 or newer is required; you have 0.9.3
上网搜了半天,说是将报错的代码注释掉,注释掉后又会出现其他问题。
在这里提供一个其他的解决方法
大多数人使用Django配置mysql数据库使用的是pymysql库,django默认使用的是mysqlclient。那么为什么不用mysqlclient呢?
在cmd命令行里更新mysqlclient库
pip install --upgrade mysqlclient
在setting.py里配置mysql
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.mysql',
'NAME': '数据库名称'
'USER': '用户名',
'PASSWORD': '用户密码',
'HOST': 'localhost',
'PORT': '3306',
}
}
配置完成后执行迁移命令
python manage.py makemigrations
python manage.py migrate
此时就可以正常执行,不会报错了
更新一个,这段时间开发环境从win10换到了mac,再配置的时候发现在mac下会出一些其他的问题。下面是mac的解决方案
我的python版本和pip版本
misslovedeMBP:lib misslove$ python3
Python 3.7.4 (v3.7.4:e09359112e, Jul 8 2019, 14:54:52)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
misslovedeMBP:lib misslove$ pip3 -V
pip 19.2.3 from /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pip (python 3.7)
使用pip命令安装django和mysqlclient
pip3 install -i https://pypi.douban.com/simple/ django
pip3 install -i https://pypi.douban.com/simple/ mysqlclient
安装完后使用python3 manage.py runserver会报错误
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
明明安装了mysqlclient却还是报错,尝试在python3里引用MySQLdb库发现报错
>>> import MySQLdb
Traceback (most recent call last):
File "", line 1, in
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in
from . import _mysql
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib
Referenced from: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so
Reason: image not found
主要看下面这一个报错,我这里因为已经改了一些东西,可能报错不一样,但都是lib***.dylib。意思是在/usr/lib目录下找不到这个文件。我们安装mysql时肯定是有的,所以建立几个软链接指向它就可以了。
Library not loaded: @rpath/libmysqlclient.21.dylib
我的mysql安装在 /usr/local/mysql-8.0.17-macos10.14-x86_64/,所以在这个目录下找lib目录,进去后会发现有刚才报错缺失的文件
misslovedeMBP:lib misslove$ cd /usr/local/mysql-8.0.17-macos10.14-x86_64/
misslovedeMBP:mysql-8.0.17-macos10.14-x86_64 misslove$ ls
LICENSE README.router docs lib share
LICENSE.router bin include man support-files
README data keyring run var
misslovedeMBP:mysql-8.0.17-macos10.14-x86_64 misslove$ cd lib/
misslovedeMBP:lib misslove$ ls
libcrypto.1.0.0.dylib libmysqlrouter_http.1.dylib
libcrypto.dylib libmysqlrouter_http.dylib
libmysqlclient.21.dylib libmysqlservices.a
libmysqlclient.a libssl.1.0.0.dylib
libmysqlclient.dylib libssl.dylib
libmysqlharness.1.dylib mecab
libmysqlharness.a mysqlrouter
libmysqlharness.dylib pkgconfig
libmysqlrouter.1.dylib plugin
libmysqlrouter.dylib
找到具体位置后就可以在/usr/lib目录下建立软链接。建立完成后可以到/usr/lib目录下使用ls -al命令查看建立的链接
sudo ln -s /usr/local/mysql-8.0.17-macos10.14-x86_64/lib/libmysqlclient.21.dylib /usr/lib/libmysqlclient.21.dylib
misslovedeMBP:lib misslove$ ls -al
lrwxr-xr-x 1 root wheel 69 9 8 15:26 libmysqlclient.21.dylib -> /usr/local/mysql-8.0.17-macos10.14-x86_64/lib/libmysqlclient.21.dylib
建立完成后再去python里import MySQLdb可能会报其他的文件问题,复制文件名,建立软链接就可以了。
除了这个问题还会有一个新的问题,报找不到MySQLdb库,说实话我看到了也一脸懵逼,发现退出重新再打开就好,或者重启终端。可能需要让系统底层代码跑一会儿?
misslovedeMBP:lib misslove$ python3
Python 3.7.4 (v3.7.4:e09359112e, Jul 8 2019, 14:54:52)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MqSQLdb
Traceback (most recent call last):
File "", line 1, in
ModuleNotFoundError: No module named 'MqSQLdb'
>>> exit()
misslovedeMBP:platform_static misslove$ python3
Python 3.7.4 (v3.7.4:e09359112e, Jul 8 2019, 14:54:52)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>
此时再python3 manage.py runserver就没问题了。
下面这个问题是因为我把win10下的整个文件夹复制粘贴到mac下的,执行一下python manage.py migrate命令就可以了,具体命令时做什么的自行百度。我就不说了,因为我也只知道作用,讲不清概念
misslovedeMBP:platform_static misslove$ python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
September 08, 2019 - 07:44:10
Django version 2.2.5, using settings 'platform_static.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.