Reputation: 337
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
Upvotes: 2
Views: 1729
Reputation: 52018
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
).
Upvotes: 0
Reputation: 128
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.
Upvotes: 0
Reputation: 8551
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
Upvotes: 1
Reputation: 690
Yes you have to reinstall the mysql for django in the virtual environment again. For windows you can do:-
pip install django mysqlclient
Upvotes: 1
Reputation: 1650
Yes, of course.You need to install 'mysqlclient' package or 'mysql-connector-python' package, with pip.
Upvotes: 2