Rectangle 27 0

python Setting Django up to use MySQL?


'ENGINE': 'mysql.connector.django',
1. OPTIONS.
2. NAME, USER, PASSWORD, HOST, PORT
3. MySQL option files.
CREATE DATABASE mydatabase CHARACTER SET utf8 COLLATE utf8_bin
DATABASES
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
            'read_default_file': '/path/to/my.cnf',
        },
    }
}
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',
    }
}
[client]
database = DB_NAME
host = localhost
user = DB_USER
password = DB_PASSWORD
default-character-set = utf8
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

If you are just testing your application on your local machine, you can use

If you are using Oracle's MySQL connector your ENGINE line should look like this:

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.

Mysql default character set is often not utf-8, therefore make sure to create your database using this sql:

With this new method of connecting in Django 1.7, it is important to know the order connections are established:

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:

You also need to create the /path/to/my.cnf file with similar settings from above

Note
Rectangle 27 0

python Setting Django up to use MySQL?


python manage.py runserver
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 :

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

Note
Rectangle 27 0

python Setting Django up to use MySQL?


# my.cnf
[client]
database = NAME
host = HOST NAME or IP
user = USER
password = PASSWORD
default-character-set = utf8
# settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
            'read_default_file': '/path/to/my.cnf',
        },
    }
}


# my.cnf
[client]
database = NAME
user = USER
password = PASSWORD
default-character-set = utf8
'OPTIONS': {
   'init_command': 'SET storage_engine=INNODB',
}

Andy's answer helps but if you have concern on exposing your database password in your django setting, I suggest to follow django official configuration on mysql connection: https://docs.djangoproject.com/en/1.7/ref/databases/

To replace 'HOST': '127.0.0.1' in setting, simply add it in my.cnf:

Note
Rectangle 27 0

python Setting Django up to use MySQL?


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

If you are using python3.x then Run below command

Note
Rectangle 27 0

python Setting Django up to use MySQL?


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django_db',
        'HOST': '127.0.0.1',
        'PORT': '3306',
        'USER': 'root',
        'PASSWORD': '123456',
    }
}
Note
Rectangle 27 0

python Setting Django up to use MySQL?


# my.cnf
[client]
database = NAME
host = HOST NAME or IP
user = USER
password = PASSWORD
default-character-set = utf8
# settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
            'read_default_file': '/path/to/my.cnf',
        },
    }
}


# my.cnf
[client]
database = NAME
user = USER
password = PASSWORD
default-character-set = utf8
'OPTIONS': {
   'init_command': 'SET storage_engine=INNODB',
}

Andy's answer helps but if you have concern on exposing your database password in your django setting, I suggest to follow django official configuration on mysql connection: https://docs.djangoproject.com/en/1.7/ref/databases/

To replace 'HOST': '127.0.0.1' in setting, simply add it in my.cnf:

Note
Rectangle 27 0

python Setting Django up to use MySQL?


DATABASES = {
 'default': {
 'ENGINE': 'django.db.backends.mysql',
 'NAME': 'YOUR_DATABASE_NAME',
 'USER': 'YOUR_MYSQL_USER',
 'PASSWORD': 'YOUR_MYSQL_PASS',
 'HOST': 'localhost',   # Or an IP that your DB is hosted on
 'PORT': '3306',
 }
}

You must create a MySQL database first. Then go to settings.py file and edit the 'DATABASES' dictionary with your MySQL credentials:

Note
Rectangle 27 0

python Setting Django up to use MySQL?


DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'DB_NAME',
    'HOST': '127.0.0.1',
    'PORT': '3306',
    'USER': 'root',
    'PASSWORD': '',
}}
http://localhost/phpmyadmin/
http://localhost:80
port 3306
settings.py
  • Create any database which you want to use on your Django Project.
  • From here you can see your databases and access them using very friendly GUI.

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

Install the following packages in the virtualenv (if you're using django on virtualenv, which is more preferred):

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

Note
Rectangle 27 0

python Setting Django up to use MySQL?


DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'django',
    'USER': 'root',
    'PASSWORD': '*****',
    'HOST': '***.***.***.***',
    'PORT': '3306',
    'OPTIONS': {
        'autocommit': True,
    },
}
auth_group
auth_group_permissions
auth_permission
auth_user
auth_user_groups
auth_user_user_permissions
django_admin_log
django_content_type
django_migrations
django_session
python manage.py migrate

this is a showcase example ,test on Django version 1.11.5: Django-pool-showcase

Note