Setting Django up to use MySQL
MySQL support is simple to add. In your DATABASES
dictionary, you will have an entry like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
You also have the option of utilizing MySQL option files, as of Django 1.7. You can accomplish this by setting your DATABASES
array like so:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '/path/to/my.cnf',
},
}
}
You also need to create the /path/to/my.cnf
file with similar settings from above
[client]
database = DB_NAME
host = localhost
user = DB_USER
password = DB_PASSWORD
default-character-set = utf8
With this new method of connecting in Django 1.7, it is important to know the order connections are established:
1. OPTIONS.
2. NAME, USER, PASSWORD, HOST, PORT
3. MySQL option files.
In other words, if you set the name of the database in OPTIONS, this will take precedence over NAME, which would override anything in a MySQL option file.
If you are just testing your application on your local machine, you can use
python manage.py runserver
Adding the ip:port
argument allows machines other than your own to access your development application. Once you are ready to deploy your application, I recommend taking a look at the chapter on Deploying Django on the djangobook
Mysql default character set is often not utf-8, therefore make sure to create your database using this sql:
CREATE DATABASE mydatabase CHARACTER SET utf8 COLLATE utf8_bin
If you are using Oracle's MySQL connector your ENGINE
line should look like this:
'ENGINE': 'mysql.connector.django',
Note that you will first need to install mysql on your OS.
brew install mysql (MacOS)
Also, the mysql client package has changed for python 3 (MySQL-Client
works only for python 2)
pip3 install mysqlclient
How to configure and use MySQL with Django?
You can easily install xampp first from https://www.apachefriends.org/download.html
(Follow these steps for xampp installation.)
Then follow the instructions as:
- Install and run xampp from http://www.unixmen.com/install-xampp-stack-ubuntu-14-04/, then start Apache Web Server and MySQL Database from the GUI.
- You can configure your web server as you want but by default web server is at
http://localhost:80
and database atport 3306
, and PhpMyadmin athttp://localhost/phpmyadmin/
- From here you can see your databases and access them using very friendly GUI.
- Create any database(
DB_NAME
) which you want to use on your Django Project. Edit your settings.py file
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB_NAME',
'HOST': '127.0.0.1',
'PORT': '3306',
'USER': 'root',
'PASSWD': '',
}}Install the following packages in the virtualenv (if you're using django on virtualenv, which is more preferred):
sudo apt-get install libmysqlclient-dev
pip install MySQL-python
That's it!! you have configured Django with MySQL in a very easy way.
Now run your Django project:
python manage.py migrate
python manage.py runserver
MySql configuration for Django
The MySql configs can be provided in settings.py file, as like below
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
'charset': 'utf8mb4',
}
} }
If need to configure with external file, below may be helpful
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': os.path.join(BASE_DIR, 'mysql.cnf'),
}
} }
The mysql.cnf
file should be in project dir
How to set up Django MySQL Database on pythonanywhere?
Check this guide. I used it to deploy my webapp.
Related Topics
Why Can't Environmental Variables Set in Python Persist
Dataframe Set_Index Not Setting
How to Make an Image with a Transparent Backround in Pygame
Python: How to Send Mail with To, Cc and Bcc
How to Set the Default Color Cycle for All Subplots with Matplotlib
Passing Csrftoken with Python Requests
How to Generate a List of Consecutive Numbers
Does a Slicing Operation Give Me a Deep or Shallow Copy
Why Is Adding Attributes to an Already Instantiated Object Allowed
How to Find Files and Skip Directories in Os.Listdir
How to Perform Two-Dimensional Interpolation Using Scipy
Pandas Sum by Groupby, But Exclude Certain Columns
Running Jupyter via Command Line on Windows
Does Tkinter Have a Table Widget
How to Strip the Whitespace from Pandas Dataframe Headers
Replace Column Values in One Dataframe by Values of Another Dataframe