[ERROR] COLLATION ‘utf8_unicode_ci‘ is not valid for CHARACTER SET ‘latin1‘

[ERROR] COLLATION 'utf8_unicode_ci' is not valid for CHARACTER SET 'latin1'

错误来源是:
跟着b站的谷粒商城项目做,前面的视频中设置了数据库的字符集编码,但是后面自己发现了MySQL容器重启报错,不停的在重启
查看log信息可以使用命令:docker logs 容器id
错误如下:

也就是说,前面我在

设置的如下字符集编码内容是有问题

[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve

删除my.cnf文件MySQL容器又可以正常启动

解决方法:

vi /mydata/mysql/conf/my.cnf

把my.cnf文件做出如下修改

[client]
default_character_set=utf8
[mysql]
default-character-set=utf8
[mysqld]
collation_server = utf8_general_ci
character_set_server = utf8
skip-character-set-client-handshake
skip-name-resolve

重启MySQL容器

docker restart mysql

进入容器内部查看

docker exec -it mysql /bin/bash

修改字符集编码成功,退出容器重启MySQL也不存在报错

默认MySQL5.7的字符集编码是:

show variables like 'character_set_%';

注意
docker安装完MySQL并run出容器后,建议请先修改完字符集编码后再新建mysql库-表-插数据