2

I have already install mysql 5.1 on my windows 10 machine , and I can connect mysql from python by :

import pymysql
conn=pymysql.connect(host='localhost',user='root',password='MYSQLTB',db='shfuture')

then I download django frame and try to use it to connect mysql , what I do is :

create a my.cnf file content is :

[client]
database = shfuture
host = localhost
user = root
password = MYSQLTB
default-character-set = utf8

change settings.py to :

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
            'read_default_file': os.path.join(BASE_DIR, 'my.cnf'),
        },
    }
}

then run :

python manage.py runserver

but got a error :

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install the MySQL client?

Do I still need to install an addition MySQL in Django virtual env? if I can use the existing MySQL instead

Jeya Suriya Muthumari
  • 1,947
  • 3
  • 25
  • 47
yangl
  • 337
  • 1
  • 3
  • 18
  • 1
    Possible duplicate of [django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb](https://stackoverflow.com/questions/15312732/django-core-exceptions-improperlyconfigured-error-loading-mysqldb-module-no-mo) – anupsabraham May 28 '19 at 05:02
  • You will need to install the client libraries to access a MySQL server, but not the MySQL server itself. Your question does not distinguish between them. – Klaus D. May 28 '19 at 05:38

5 Answers5

2

Yes, of course.You need to install 'mysqlclient' package or 'mysql-connector-python' package, with pip.

Milad Hatami
  • 1,494
  • 13
  • 18
1

I guess you dont have mysqlclient python library installed in virtual environment.

Since you are using Windows, you need to download and install mysqlclient python library from here

Jeril
  • 7,858
  • 3
  • 52
  • 69
1

Yes you have to reinstall the mysql for django in the virtual environment again. For windows you can do:-

pip install django mysqlclient

Suraj Kumar
  • 690
  • 5
  • 6
0

Basically you need to put the following code on top of both manage.py and wsgi.py like this:

#!/usr/bin/env python
import os
import sys
import pymysql
pymysql.install_as_MySQLdb()
# rest of the code

Also you need to ensure you have pymysql installed in your virtual environment.

FYI: Using pymysql might not work in django>=2.2 properly. You will find some solution which might work, like mentioned in this ticket. Also, django does not support pymysql as mysql connector officially(ticket #12500, #22391).

ruddra
  • 50,746
  • 7
  • 78
  • 101
0

Both PyMySQL and MySQLdb are db connectors. I assume that your django framework by default searches for MySQLdb and throws exception because it couldn't find one.

You can either install MySQLdb or follow @ruddra's answer on that. I would recommend you to go with @ruddra's answer incase of development machine. Because there are lot of issues you might(50/50 chances) face while installing and making MySQLdb to work properly.

Read What is PyMySQL and how does it differ from MySQLdb? Can it affect Django deployment? to understand more on this.