MySQL 字符集
讨论环境
- windows10
- mysql5.7.24
- powershell
从客户端连接到 mysql 服务器角度
powershell 终端用 mysql 客户端连接 mysql 服务器
查看 powershell 使用的字符集
活动代码页: 936 # 请注意活动代码页这是 gbk
936
PS C:\Users\young> mysql -uroot -p
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.24-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
由上述连接可知 mysql 客户端使用的字符集是
gb2312
(也就是活动页是 936)。① 这就是本次这个 mysql 客户端使用字符集
。
mysql 连接 mysqld 数据包分析
mysql login 流程(抓包查看)
(数据包交互流程)
查看第 4 个 Server Greeting 数据包
可以看到 mysql 客户端和 mysql 服务器建立 TCP 连接后, mysql 服务器主动发起 Server Greeting,在这个数据包里,说明了 mysql 服务器使用的字符集。
如何查看 mysql 服务器使用的字符集
mysql> show variables like '%server';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| character_set_server | latin1 | # mysql 服务器启动时使用的字符集
| collation_server | latin1_swedish_ci | # 使用的字符集采用的排序规则。
+----------------------+-------------------+
2 rows in set, 1 warning (0.00 sec)
查看第 6 个 Login Request 数据包
可以看到这个请求把 powershell 终端,也就是
mysql 客户端
使用的字符集告知了 mysql 服务器。这样服务器就可以根据这个字符集,设置字符集相关的变量了。
mysql 服务器哪些字符设置和 mysql 客户端使用的字符集有关?
mysql> show variables like 'character%';
+--------------------------+---------------------------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------------------------+
| character_set_client | gbk (注: mysql 客户端使用的字符集) |
| character_set_connection | gbk (注: mysql 服务器处理请求用到的字符集) |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | gbk (注: mysql 服务器响应使用的字符集) |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | C:\Program Files\MySQL\MySQL Server 5.7\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set, 1 warning (0.01 sec)
可以看到服务器根据 mysql 客户端使用的字符集 gbk 设置了
character_set_client
这个Session
级别的变量。
#### 啥是 Session 级别的变量?
character_set_client
这个就是 session 级别的变量。因为不同的 mysql 终端使用的字符集可能并不一样。
举例说明
Active code page: 65001 # 使用 chcp 65001 将 powershell 使用的字符集调为 utf-8
PS C:\Users\young> mysql -uroot -p
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.24-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show variables like 'character%';
+--------------------------+---------------------------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------------------------+
| character_set_client | utf8 (1) |
| character_set_connection | utf8 (2) |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 (3) |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | C:\Program Files\MySQL\MySQL Server 5.7\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set, 1 warning (0.01 sec)
可以看到 powershell 调整是使用的字符集为 utf-8 后,相应地,
character_set_client
这个变量也设置为了 utf8。这就是 Session 级别的变量。