在docker-compose.yml文件中定义mysql导入utf-8的万国码
services:
mysql:
image:mysql:5.7
# command: ['--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']
volumes:
-./data/docker/mysql:/var/lib/mysql
-./mysql/:/docker-entrypoint-initdb.d/
-./conf/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
-./sqlfile:/app/sqlfile
environment:
-"MYSQL_DATABASE=apigateway-ai"
-"MYSQL_USER=appuser"
-"MYSQL_PASSWORD=123456"
-"MYSQL_ROOT_PASSWORD=root"
ports:
-"3306:3306"
restart:always
logging:
driver: "json-file"
options:
max-size: “1g”
启动spring Boot框架后,web平台显示乱码。
进入mysql容器,查看字符集mysql> show variables like '%char%';
发现character_set_client和character_set_server等字符集仍然不是utf-8;
解决:在宿主机的cnf文件中定义字符集default-character-set = utf8mb4,再映射到容器里,同时取消compose文件中导入utf-8的command命令。
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[client]
default-character-set = utf8mb4 #定义字符集
[mysql]
default-character-set = utf8mb4 #定义字符集
[mysqld]
pid-file= /var/run/mysqld/mysqld.pid
socket= /var/run/mysqld/mysqld.sock
datadir= /var/lib/mysql
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
lower-case-table-names=1 #解决数据库读取区分大小写问题
重启docker后,web平台显示正常。
结束。