Search code examples
pythonmysqldjangodjango-database

Django 500 Internal Server Error - ImproperlyConfigured: Error loading MySQLdb module:


Im new to using MySQL in general and as a database for Django. I have followed a number of different tutorials and the documentation but keep getting a 500 Internal Server Error when i deploy to my production server. It works fine on my development machine.

What am I missing? is there a setting I should change or a step i missed?

Thanks

The Error logs

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

My Setup

  • Ubuntu VM running 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, for debian-linux-gnu (x86_64) using readline 6.2

My settings.py

DATABASES = {
    'default': { 
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'django_db',                                       
        'USER': 'root',
        'PASSWORD': 'my_password',
        'HOST': '127.0.0.1',
        'PORT': '', 
    }
}

I have created the database on my production environment

CREATE DATABASE django_db;
Query OK, 1 row affected (0.01 sec) 
mysql>

I then ran syncdb

(bias_experiment)Macintosh-2:src user$ python manage.py syncdb
Creating tables ...
Creating table django_admin_log
....

Solution

  • This line:

    ImproperlyConfigured: Error loading MySQLdb module: this is MySQLdb version (1, 2, 5, 'final', 1), but _mysql is version (1, 2, 3, 'final', 0) 
    

    indicates you might have a version mismatch between MysqlDB and MySQL. Sounds like looking in to it and reinstalling your dependent libraries resolved the issue.

    To describe the issue in further detail:

    In this case apt-get was installing MySQL-python==1.2.3. The latest version is MySQL-python==1.2.5. However apt-get was not finding it, so completely removing MySQL-python==1.2.3 using:

    sudo apt-get remove --purge python-mysqldb
    

    and then reinstall via pip

    sudo pip install mysql-python
    

    (Note the package names are slightly different)