我正在尝试将我的 django 项目中的数据库设置从 sqlite3 更改为 mysql。
我在 settings.py 文件中编辑了数据库对象:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Identity',
'USER' : 'root',
'PASSWORD': ''
}
}
我运行 django-admin dbshell 并得到这个错误:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/conf/init.py", line 39, in _setup % (desc, ENVIRONMENT_VARIABLE)) django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
我按照此 answer to use settings.configure() 中的说明进行操作
from django.conf import settings
settings.configure()
它返回了这个消息:
Traceback (most recent call last): File "", line 1, in File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/conf/init.py", line 63, in configure raise RuntimeError('Settings already configured.') RuntimeError: Settings already configured.
当我运行 python3 manage.py shell
时,它给了我这个错误:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 28, in raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'
我只想使用 mySql 而不是 sqlite db。
我该怎么做?
最佳答案
似乎 MySQLdb 包丢失了, 你能检查一下这个包是否安装了吗
apt-get install mysql-server
apt-get install mysql-client
apt-get install libmysqlclient-dev
在你的虚拟环境中,安装好mysql客户端
pip install mysqlclient
然后在settings.py中进行mysql配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test_db',#database name
'HOST': '127.0.0.1',#
'PORT': '3306',#mysql port
'USER': 'root',#mysql username
'PASSWORD': 'test123',#mysql password
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
'charset': 'utf8mb4',
}
}
}
关于mysql - 请求设置 DATABASES,但在我的 Django 项目中设置未配置错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46146533/