数据库
首页 > 数据库> > python-Django 500内部服务器错误-配置不正确:加载MySQLdb模块时出错:

python-Django 500内部服务器错误-配置不正确:加载MySQLdb模块时出错:

作者:互联网

我刚开始使用MySQL作为Django的数据库.我遵循了许多不同的教程和文档,但是当我将其部署到生产服务器时,始终收到500 Internal Server Error.在我的开发机器上工作正常.

我想念什么?有应更改的设置或错过的步骤吗?

谢谢

错误日志

[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)

我的设定

>运行12.04.5 LTS的Ubuntu VM
> Apache / 2.2.22(Ubuntu)
> Python 2.7.3
> virtualenv == 1.7.1.2
> Django == 1.6
> MySQL-python == 1.2.3
> mysql版本14.14 Distrib 5.5.38,适用于使用readline 6.2的debian-linux-gnu(x86_64)

我的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
....

解决方法:

这行:

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

(请注意,程序包名称略有不同)

标签:python,mysql,django,django-database
来源: https://codeday.me/bug/20191012/1901707.html