lothakim
lothakim

Reputation: 93

How do I use MYSQL as the database in a Django project?

The default database of the Django is sqlite, however I want to use MYSQL instead.
Since the MYSQLdb module is not supported in python3.x, the official doc of django recommend using mysqlclient and MySQL Connector/Pythoninstead.Here is the original doc:

MySQL has a couple drivers that implement the Python Database API described in PEP 249:
• mysqlclient is a native driver. It’s the recommended choice.
• MySQL Connector/Python is a pure Python driver from Oracle that does not require the MySQL client library or any Python modules outside the standard library.
These drivers are thread-safe and provide connection pooling.
In addition to a DB API driver, Django needs an adapter to access the database drivers from its ORM. Django provides an adapter for mysqlclient while MySQL Connector/Python includes its own.

I've got the latest version of mysql-client and mysql-connector-python, but as I execute themigratecommand, error occurs.Here is part of the message:

Unhandled exception in thread started by .wrapper at 0x7f2112e99d90>
Traceback (most recent call last): File "/home/lothakim/anaconda3/envs/py36/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 15, in
import MySQLdb as Database
ModuleNotFoundError: No module named'MySQLdb'django.
.........
core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

It seems to be the problem of the database connection.But I followed every step of the official tutorial.How can I fix this problem?

Upvotes: 3

Views: 1002

Answers (1)

lothakim
lothakim

Reputation: 93

It's a silly mistake...
I confuse the mysql-client with mysqlclient.The former is part of the MYSQL application, while the latter is a python module.I didn't install the latter.
Also note you should sudo apt-get install libmysqlclient-devbefore pip install mysqlclient.

Upvotes: 1

Related Questions