Django的MySQL Driver配置

PEP 249规定了Python的数据库API。MySQL主要有三种API实现:

  • MySQLdb 是Andy Dustman开发的、使用原生代码/C语言绑定的驱动,它已经开发了数十年。
  • mysqlclient 是MySQLdb的一个支持Python3的fork,并且可以无缝替换调MySQLdb。mysqlclient目前是MySQL在Django下的推荐选择。
  • MySQL Connector/Python 是Oracle写的,纯Python实现的客户端库。

以上所有的驱动都是线程安全的,且提供了连接池。MySQLdb 是唯一一个不支持Python3的。

如果你使用mysqlclient

settings.py中的配置如下:

 1 # Database
 2 # https://docs.djangoproject.com/en/1.10/ref/settings/#databases
 3 
 4 DATABASES = {
 5     'default': {
 6         'ENGINE': 'django.db.backends.mysql', #数据库引擎
 7         'NAME': 'test',                       #数据库名
 8         'USER': 'root',                       #用户名
 9         'PASSWORD': 'root',                   #密码
10         'HOST': '',                           #数据库主机,默认为localhost
11         'PORT': '',                           #数据库端口,MySQL默认为3306
12         'OPTIONS': {
13             'autocommit': True,
14             'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
15         },
16     }
17 }

第14行主要是为了防止警告:

 (mysql.W002) MySQL Strict Mode is not set for database connection 'default' 

然后运行migrate :

 1 Operations to perform:
 2   Apply all migrations: admin, auth, contenttypes, sessions, users
 3 Running migrations:
 4   Applying contenttypes.0001_initial... OK
 5   Applying contenttypes.0002_remove_content_type_name... OK
 6   Applying auth.0001_initial... OK
 7   Applying auth.0002_alter_permission_name_max_length... OK
 8   Applying auth.0003_alter_user_email_max_length... OK
 9   Applying auth.0004_alter_user_username_opts... OK
10   Applying auth.0005_alter_user_last_login_null... OK
11 。。。

 

posted on 2017-12-18 15:47  LOVESTYUDY  阅读(1219)  评论(0编辑  收藏  举报

导航