1、查询容器ID
docker ps -a
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d8b9eadcc7e4 mysql:latest "docker-entrypoint.s…" 2 hours ago Up 3 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
2、进入容器并登录数据库
[root@docker ~]# docker exec -it mysql bash
bash-4.4# mysql -uroot -p123456
mysql> show databases;
Database |
---|
information_schema |
mysql |
performance_schema |
sys |
test |
mysql> use test;
mysql> show tables;
Tables_in_test |
---|
Dog |
Movies |
?? |
查询数据
mysql> select * from Dog; | ||||||
---|---|---|---|---|---|---|
Id | Name(??? | Weight???? | Height (??? | Type (??) | Age (??) | Color (??) |
1 | Mike | 3 | 28 | ??? | 10 | ? |
2 | Sala | 6.5 | 40 | ?? | 15 | ? |
3 | ?? | 21.5 | 45 | ?? | 26 | ? |
4 | ?? | 15 | 42 | ??? | 20 | ? |
5 | Boy | 5.5 | 24 | ??? | 6 | ? |
发现出现乱码’?‘
3、查询默认编码
mysql> show variables like '%char%'; | |
---|---|
Variable_name | Value |
character_set_client | latin1 |
character_set_connection | latin1 |
character_set_database | utf8mb4 |
character_set_filesystem | binary |
character_set_results | latin1 |
character_set_server | utf8mb4 |
character_set_system | utf8mb3 |
character_sets_dir | /usr/share/mysql-8.0/charsets/ |
需要将编码改为utf8mb3
4、修改my.cnf参数
将容器的my.cnf文件复制出来
[root@docker ~]# docker cp d8b9eadcc7e4:/etc/my.cnf /root/
编辑my.cnf文件,添加如下内容
[root@docker ~]# vi my.cnf
[mysql]
default-character-set=utf8
[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
[client]
default-character-set=utf8
将文件复制回去
docker cp /root/my.cnf d8b9eadcc7e4:/etc/
重启容器
docker restart mysql
再次查看编码发现修改成功
mysql> show variables like '%char%'; | |
---|---|
Variable_name | Value |
character_set_client | utf8mb3 |
character_set_connection | utf8mb3 |
character_set_database | utf8mb4 |
character_set_filesystem | binary |
character_set_results | utf8mb3 |
character_set_server | utf8mb3 |
character_set_system | utf8mb3 |
character_sets_dir | /usr/share/mysql-8.0/charsets/ |
查询数据
mysql> show tables; |
---|
Tables_in_test |
Dog |
Movies |
域名 |
mysql> select * from Dog; | ||||||
---|---|---|---|---|---|---|
Id | Name(名字) | Weight(体重) | Height (身高) | Type (品种) | Age (月龄) | Color (肤色) |
1 | Mike | 3 | 28 | 吉娃娃 | 10 | 白 |
2 | Sala | 6.5 | 40 | 柴犬 | 15 | 黄 |
3 | 黑狮 | 21.5 | 45 | 藏獒 | 26 | 黑 |
4 | 大圣 | 15 | 42 | 牧羊犬 | 20 | 黄 |
5 | Boy | 5.5 | 24 | 蝴蝶犬 | 6 | 白 |
显示正常
0 条评论