Django + mysql 在创建数据库出错

时间:2022-01-13 01:21:08
错误:
django.db.utils.OperationalError: (1366, "Incorrect string value: '\\xE6\\x96\\x87\\xE7\\xAB\\xA0...' for column 'name' at row 9")

  

解决方法:

  

进入mysql命令行

mysql> show variables like '%char%';
+--------------------------+-----------------------------------------------------------+
| Variable_name | Value |
+--------------------------+-----------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.7.21-macos10.13-x86_64/share/charsets/ |
如上,其中的编码有latin1的所以需要修改latin1为utf8,以下是mac中的修改办法

vim /etc/mysql/my.cnf
添加如下内容

[client]

default-character-set =utf8

[mysql]

default-character-set =utf8

[mysqld]

collation-server = utf8_unicode_ci
init_connect ='SET NAMES utf8'
character_set_server=utf8
然后重启mysql服务 并且 删除原先创建的数据库,重新创建