1.更改 /etc/mysql/my.cnf, 添加如下内容:
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
2.重启mysql
service mysqld status
3.查看msy编码
SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';
5.修改数据库编码
ALTER DATABASE 数据库名(自行替换) CHARACTER SET = utf8mb4 COLLATE utf8mb4_unicode_ci;
6.修改数据库表编码
ALTER TABLE 表名(自行更换) CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
7,修改django的settings.py,在数据库的配置中加入options项。
DATABASES = {
'default': {
#'ENGINE': 'sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'ENGINE': 'django.db.backends.mysql',
#'NAME': '/opt/media/session.db', # Or path to database file if using sqlite3.
'NAME': DATABASE_NAME,
'USER': 'root', # Not used with sqlite3.
'PASSWORD': DATABASE_PASSWORD, # Not used with sqlite3.
'HOST': DATABASE_HOST, # Set to empty string for localhost. Not used with sqlite3.
'PORT': DATABASE_PORT, # Set to empty string for default. Not used with sqlite3.
'OPTIONS': {'charset':'utf8mb4'},
},
}
8.重启