docker 安装MySQL中文乱码问题
1 启动MySQLdocker run -p 3306:3306 --name mysql04 -e MYSQL_ROOT_PASSWORD=123456 -d mysql2 进入容器[root@zhangyu sbin]# docker exec -it mysql04 /bin/bashmysql04指定的–name3 进入mysqlroot@e34aba02c0c3:/# m...
·
1 启动MySQL
docker run -p 3306:3306 --name mysql04 -e MYSQL_ROOT_PASSWORD=123456 -d mysql
2 进入容器
[root@zhangyu sbin]# docker exec -it mysql04 /bin/bash
mysql04指定的–name
3 进入mysql
root@e34aba02c0c3:/# mysql -uroot -p123456
4 查看数据库字符集
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连接层上
5 修改为utf-8
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;
6 修改配置文件
- 进入容器
[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
- [mysqld] 标签下加上2行
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
7 查看是否成功
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.
更多推荐
已为社区贡献1条内容
所有评论(0)