docker run -p 3306:3306 --name mysql04 -e MYSQL_ROOT_PASSWORD=123456 -d mysql
[root@zhangyu sbin]# docker exec -it mysql04 /bin/bash
mysql04指定的–name
root@e34aba02c0c3:/# mysql -uroot -p123456
mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+--------------------------------+
| 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 | utf8 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
默认是瑞典latin1
mysql> SHOW VARIABLES LIKE 'collation_%';
+----------------------+--------------------+
| Variable_name | Value |
+----------------------+--------------------+
| collation_connection | latin1_swedish_ci |
| collation_database | utf8mb4_0900_ai_ci |
| collation_server | utf8mb4_0900_ai_ci |
+----------------------+--------------------+
3 rows in set (0.00 sec)
connection 就是我们通过客户端连接的时候指定的编码。 外部访问数据乱码的问题就出在这个connection连接层上
mysql> SET NAMES 'utf8';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> SHOW VARIABLES LIKE 'collation_%';
+----------------------+--------------------+
| Variable_name | Value |
+----------------------+--------------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8mb4_0900_ai_ci |
| collation_server | utf8mb4_0900_ai_ci |
+----------------------+--------------------+
3 rows in set (0.00 sec)
SET NAMES 'utf8';
它相当于下面的三句指令:
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;
[root@zhangyu ~]# docker exec -it mysql04 /bin/bash
root@e34aba02c0c3:/# cd /etc/mysql
root@e34aba02c0c3:/etc/mysql# ls
conf.d my.cnf my.cnf.fallback
root@e34aba02c0c3:/etc/mysql# vim my.cnf
bash: vim: command not found
vim: command not found ,需要安装vim
3. 安装vim
root@e34aba02c0c3:/etc/mysql# apt-get update
root@e34aba02c0c3:/etc/mysql# apt-get install vim
4.修改my.cnf
default-character-set = utf8
character_set_server = utf8
[mysql] 标签下加上一行
default-character-set = utf8
[mysql.server]标签下加上一行
default-character-set = utf8
[mysqld_safe]标签下加上一行
default-character-set = utf8
[client]标签下加上一行
default-character-set = utf8
mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
和上面对比已经成功修改。
mysql> select * from department;
+----+----------------+
| id | departmentName |
+----+----------------+
| 1 | 业务部 |
| 2 | 前台部 |
+----+----------------+
ok.