Integrating Django with MySQL is straightforward. Within the DATABASES dictionary, create an entry resembling the following:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # MySQL database engine 'NAME': 'DB_NAME', # Database name 'USER': 'DB_USER', # Database user 'PASSWORD': 'DB_PASSWORD', # Database password 'HOST': 'localhost', # Host IP or 'localhost' 'PORT': '3306', # Default MySQL port } }
Alternatively, Django 1.7 allows you to utilize MySQL option files:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'OPTIONS': { 'read_default_file': '/path/to/my.cnf', # Path to MySQL option file }, } }
Within the /path/to/my.cnf file, specify the same parameters as before:
[client] database = DB_NAME host = localhost user = DB_USER password = DB_PASSWORD default-character-set = utf8
Please note the order in which connections are established:
For local testing, simply run:
python manage.py runserver
Adding the ip:port argument allows external access to your application. For production deployment, refer to the djangobook's "Deploying Django" chapter.
Ensure that your database character set is UTF-8 by creating it using the following SQL:
CREATE DATABASE mydatabase CHARACTER SET utf8 COLLATE utf8_bin
If using Oracle's MySQL connector for Python 3, your ENGINE line should be:
'ENGINE': 'mysql.connector.django',
Install MySQL on your OS:
brew install mysql (MacOS)
Ensure you have the correct Python client installed (for Python 3):
pip3 install mysqlclient
The above is the detailed content of How Do I Configure Django to Use MySQL?. For more information, please follow other related articles on the PHP Chinese website!