python - Setting Django up to use MySQL

ID : 20086

viewed : 38

Tags : pythonmysqldjangodebianpython

Top 5 Answer for python - Setting Django up to use MySQL

vote vote

93

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 
vote vote

90

To the very first please run the below commands to install python dependencies otherwise python runserver command will throw error.

sudo apt-get install libmysqlclient-dev sudo pip install MySQL-python 

Then configure the settings.py file as defined by #Andy and at the last execute :

python manage.py runserver 

Have fun..!!

vote vote

75

If you are using python3.x then Run below command

pip install mysqlclient 

Then change setting.py like

DATABASES = { 'default': {     'ENGINE': 'django.db.backends.mysql',     'NAME': 'DB',      'USER': 'username',     'PASSWORD': 'passwd',   }   } 
vote vote

64

As all said above, you can easily install xampp first from https://www.apachefriends.org/download.html Then follow the instructions as:

  1. 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.
  2. You can configure your web server as you want but by default web server is at http://localhost:80 and database at port 3306, and PhpMyadmin at http://localhost/phpmyadmin/
  3. From here you can see your databases and access them using very friendly GUI.
  4. Create any database which you want to use on your Django Project.
  5. Edit your settings.py file like:

    DATABASES = { 'default': {     'ENGINE': 'django.db.backends.mysql',     'NAME': 'DB_NAME',     'HOST': '127.0.0.1',     'PORT': '3306',     'USER': 'root',     'PASSWORD': '', }} 
  6. 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

  7. That's it!! you have configured Django with MySQL in a very easy way.

  8. Now run your Django project:

    python manage.py migrate

    python manage.py runserver

vote vote

59

Actually, there are many issues with different environments, python versions, so on. You might also need to install python dev files, so to 'brute-force' the installation I would run all of these:

sudo apt-get install python-dev python3-dev sudo apt-get install libmysqlclient-dev pip install MySQL-python pip install pymysql pip install mysqlclient 

You should be good to go with the accepted answer. And can remove the unnecessary packages if that's important to you.

Top 3 video Explaining python - Setting Django up to use MySQL

Related QUESTION?