三步走解决:
1.修改Django的settings
2.修改mariadb的配置文件
3.重启mysql服务
大坑:原来的数据库mariadb修改配置文件为utf8后,原来的数据库test还是沿用的原来的字符集latin1,所以需要创建新的数据库test2,这样Django ORM再次黄建时候就会默认创建urf8的表。
修改settings
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'OPTIONS': {'charset': 'utf8'}, #增加选项 'NAME': 'test2', 'USER': 'soymilk', 'PASSWORD': '123456', 'HOST': '172.16.241.2', 'PORT': '3306', } }
修改mariadb配置文件
/etc/my.cnf
[mysqld] character-set-server=utf8 collation-server=utf8_unicode_ci
/etc/my.cnf.d/mysql-clients.cnf
[mysql]
default-character-set=utf8
重启mariadb
systemctl restart mariaDB
再次查看确认
SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
+--------------------------+--------------------+
| Variable_name | Value |
+--------------------------+--------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| collation_connection | utf8mb4_unicode_ci |
| collation_database | utf8mb4_unicode_ci |
| collation_server | utf8mb4_unicode_ci |
+--------------------------+--------------------+
10 rows in set (0.00 sec)
大坑:
新建数据库test2
create database test2;
然后直接用ORM同步
mysql解决的话,修改的配置文件不一样(待验证)
/etc/mysql/my.cnf
[mysql] default-character-set=utf8mb4 [mysqld] character-set-server=utf8mb4 collation-server=utf8mb4_unicode_ci
参考:
https://stackoverflow.com/questions/1073295/django-character-set-with-mysql-weirdness 解决Django中文乱码
https://blog.csdn.net/DaLiShuiJiao/article/details/78122802 解决mariadb中文乱码问题