-
1、安装pymysql,因为python3.5没有mysqldb包
-
pip install pymysql
2、在settings.py中添加下面内容: -
import pymysql pymysql.install_as_MySQLdb()
-
- 3、将settings里面的DATABASES改成MySQL相关
-
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'OPTIONS':{'read_default_file': os.path.join(BASE_DIR, 'my.cnf'),} } }
-
4、在settings的上一级目录中添加 my.cnf 文件,内容如下:
-
[client] database = 'miniblog' user = 'root' password = '123456' default-character-set = utf8
-
5、进入mysql,查看是否有 miniblog数据库,没有的话需手动创建
-
mysql -u root -p show databases; create database miniblog;
6、接下来就可以正常的makemigrations、migrate等了。 -
python manage.py makemigrations python manage.py migrate
7、文件结构如下: -
[willie@localhost miniblog]$ find . -name *~ -exec rm -f {} \; [willie@localhost miniblog]$ tree . ├── account │ ├── admin.py │ ├── admin.pyc │ ├── apps.py │ ├── forms.py │ ├── forms.pyc │ ├── __init__.py │ ├── __init__.pyc │ ├── migrations │ │ ├── 0001_initial.py │ │ ├── 0001_initial.pyc │ │ ├── __init__.py │ │ └── __init__.pyc │ ├── models.py │ ├── models.pyc │ ├── templates │ │ ├── account │ │ │ ├── account_base.html │ │ │ └── register.html │ │ └── account_base.html │ ├── tests.py │ ├── urls.py │ ├── urls.pyc │ ├── views.py │ └── views.pyc ├── blog │ ├── admin.py │ ├── admin.pyc │ ├── apps.py │ ├── __init__.py │ ├── __init__.pyc │ ├── migrations │ │ ├── __init__.py │ │ └── __init__.pyc │ ├── models.py │ ├── models.pyc │ ├── tests.py │ ├── views.py │ └── views.pyc ├── manage.py ├── miniblog │ ├── __init__.py │ ├── __init__.pyc │ ├── settings.py │ ├── settings.pyc │ ├── urls.py │ ├── urls.pyc │ ├── wsgi.py │ └── wsgi.pyc ├── my.cnf └── templates └── index.html 8 directories, 44 files
python django1.10.4 连接mysql (使用 pymysql)
最新推荐文章于 2024-03-12 13:03:12 发布