Django安装mysql

系统安装mysql-devel

For Ubuntu:
sudo apt-get update
sudo apt-get install python-dev
sudo apt-get install python-MySQLdb
For CentOS:
yum install python-devel mysql-devel
安装Python模块
pip3 install mysqlclient

异常 1. 无索引权限 migrate的时候提示:
django.db.utils.OperationalError: (1142, "INDEX command denied to user 'manager'@'10.**.***.10 ' for table 'django_session'")

提示用户没有 索引权限,开放权限即可。
参考自:django1-8-11-migrate-installed-apps-error
2. MySQL未为数据库连接设置“默认”严格模式
WARNINGS: ?: (Mysql.W002) MySQL Strict Mode is not set for database connection 'default' Hint: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/1.11/ Ref / databases / # mysql-sql-mode CommandError: App 'exams' does not have migrations.

因为MySQL未为数据库连接设置“默认”严格模式导致exams模块迁移失败。
settings设置中为数据库配置添加OPTIONS即可:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'my_database', 'OPTIONS': { 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'", }, } }

参考:
Django-MySQL checks
CSDN 相关文章
3. 编码问题 使用 python manage.py loaddata ***.json 数据迁移的时候遇到问题:
提示 incorrect string value” error when save unicode string in Django
网上给的解决办法是 settings中添加数据库配置:
'OPTIONS': { 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'", 'charset': 'utf8mb4', },

再次 python manage.py loaddata ***.json,提示:Can't initialize character set utf8mb4
命令行 修改MYSQL表中字段字符集
ALTER TABLE share_zodiacshare MODIFY COLUMN zodiac_chinese VARCHAR(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

修改字符集不会对已存在数据生效,因此删除数据、修改字符集后,再次迁移数据正常通过并且无乱码。
MYSQL 字符集 相关命令
参考:
【Django安装mysql】pip-install-mysql-python-fails-with-environmenterror-mysql-config-not-found

    推荐阅读