Django 500内部服务器错误 - 配置错误:加载MySQLdb模块时出错
我刚开始使用MySQL,特别是作为Django的数据库。我看了很多不同的教程和文档,但在我把项目部署到生产服务器时,总是出现500内部服务器错误。在我的开发机器上运行得很好。
我漏掉了什么吗?有没有什么设置需要更改,或者我错过了哪个步骤?
谢谢
错误日志
[Mon Aug 18 19:23:10 2014] [error] [client 134.226.38.233] return getattr(connections[DEFAULT_DB_ALIAS], item)
[Mon Aug 18 19:23:10 2014] [error] [client 134.226.38.233] File "/var/www/bias_experiment/lib/python2.7/site-packages/django/db/utils.py", line 198, in __getitem__
[Mon Aug 18 19:23:10 2014] [error] [client 134.226.38.233] backend = load_backend(db['ENGINE'])
[Mon Aug 18 19:23:10 2014] [error] [client 134.226.38.233] File "/var/www/bias_experiment/lib/python2.7/site-packages/django/db/utils.py", line 113, in load_backend
[Mon Aug 18 19:23:10 2014] [error] [client 134.226.38.233] return import_module('%s.base' % backend_name)
[Mon Aug 18 19:23:10 2014] [error] [client 134.226.38.233] File "/var/www/bias_experiment/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
[Mon Aug 18 19:23:10 2014] [error] [client 134.226.38.233] __import__(name)
[Mon Aug 18 19:23:10 2014] [error] [client 134.226.38.233] File "/var/www/bias_experiment/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 17, in <module>
[Mon Aug 18 19:23:10 2014] [error] [client 134.226.38.233] raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
[Mon Aug 18 19:23:10 2014] [error] [client 134.226.38.233] ImproperlyConfigured: Error loading MySQLdb module: this is MySQLdb version (1, 2, 5, 'final', 1), but _mysql is version (1, 2, 3, 'final', 0)
我的环境设置
- 运行Ubuntu 12.04.5 LTS的虚拟机
- Apache/2.2.22(Ubuntu)
- Python 2.7.3
- virtualenv==1.7.1.2
- Django==1.6
- MySQL-python==1.2.3
- mysql Ver 14.14 Distrib 5.5.38,适用于debian-linux-gnu(x86_64),使用readline 6.2
我的settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_db',
'USER': 'root',
'PASSWORD': 'my_password',
'HOST': '127.0.0.1',
'PORT': '',
}
}
我已经在生产环境中创建了数据库
CREATE DATABASE django_db;
Query OK, 1 row affected (0.01 sec)
mysql>
然后我运行了syncdb
(bias_experiment)Macintosh-2:src user$ python manage.py syncdb
Creating tables ...
Creating table django_admin_log
....
2 个回答
1
为了帮助可能遇到同样错误的朋友们:
我也遇到了同样的错误,但我的MySQL-python版本和预期的一样,都是1.2.5。问题出在我把MySQLdb文件夹的链接放在了项目的根目录里。后来我把这个链接的文件夹移动到了存放Django设置的文件夹里,这样问题就解决了。
1
这一行:
ImproperlyConfigured: Error loading MySQLdb module: this is MySQLdb version (1, 2, 5, 'final', 1), but _mysql is version (1, 2, 3, 'final', 0)
说明你的MysqlDB和MySQL之间可能存在版本不匹配的问题。听起来你检查了一下,重新安装了相关的库,问题就解决了。
更详细地描述这个问题:
在这个例子中,apt-get正在安装MySQL-python==1.2.3,而最新的版本是MySQL-python==1.2.5。不过apt-get没有找到最新版本,所以你需要先完全卸载MySQL-python==1.2.3,使用:
sudo apt-get remove --purge python-mysqldb
然后通过pip重新安装
sudo pip install mysql-python
(注意包的名称稍微有点不同)