Django 配置MySQL数据库

来自linux中国网wiki
跳到导航 跳到搜索

代码


apt install mysql-server mysql-client
mysql_secure_installation
apt-get install python3-dev default-libmysqlclient-dev build-essential pkg-config apt-get install python-dev libmysqld-dev libmysqlclient-dev

pip install mysql-python
pip3 install mysqlclient


Django(Python)操作MySQL依赖第三方包,所以要先安装MySQL for Python。

$ wget https://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.5.zip
$ unzip MySQL-python-1.2.5.zip
$ sudo python setup.py install

#配置 
# #CREATE DATABASE ch07www CHARACTER SET utf8;
# settings.py  Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'django',                      # 得先建库
        # The following settings are not used with sqlite3:
        'USER': 'root',
        'PASSWORD': '1234',
        'HOST': '127.0.0.1',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '3306',                      # Set to empty string for default.
    }
}

参考

https://docs.djangoproject.com/en/1.11/ref/databases/#mysql-notes

DJANGO+MYSQL安装配置详解(LINUX)[更新为1.8.2版]

https://docs.djangoproject.com/en/1.8/ref/settings/#databases

https://shenxgan.gitbooks.io/django/content/publish/2015-07-13-django-mysql.html