docker启动的mysql8中文乱码问题和无法输入中文

时间:2024-04-28 12:44:37

问题描述:

1.中文显示乱码
在这里插入图片描述
2.无法输入中文

中文乱码临时方案:

show variables like ‘character%’;
SET NAMES utf8mb4;
SET CHARACTER SET utf8mb4;

中文乱码永久方案:

vim /etc/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

无法输入中文解决方案:

添加环境变量:
env LANG=C.UTF-8

docker exec -it b18f56aa1e15 env LANG=C.UTF-8 /bin/bash

docker-compose.yml

 mysql:
    container_name: secark-mysql
    image: mysql:8.2.0
    # network_mode: host
    environment:
      - MYSQL_ROOT_PASSWORD=xxxxxxxx. # mysql密码
      - LANG=C.UTF-8
    volumes:
      - /etc/localtime:/etc/localtime  
      - ../configs/mysql/init:/docker-entrypoint-initdb.d/  # 初始化sql文件
      - ../configs/mysql/config/my.cnf:/etc/my.cnf   # mysql配置文件
      - ../../volumes/mysql:/var/lib/mysql    # mysql 持久化数据
    restart: always
    ports:
      - '3306:3306'

my.cnf文件

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.2/en/server-configuration-defaults.html
[client]
default-character-set=utf8mb4

[mysql]
default-character-set=utf8mb4

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.2/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
character-set-client-handshake=FALSE
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
# 关闭logbin
skip-log-bin
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql

pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock

!includedir /etc/mysql/conf.d/